diff --git a/src/main/java/org/ntlab/citrusserver/entities/Account.java b/src/main/java/org/ntlab/citrusserver/entities/Account.java index 0567713..eb04687 100644 --- a/src/main/java/org/ntlab/citrusserver/entities/Account.java +++ b/src/main/java/org/ntlab/citrusserver/entities/Account.java @@ -11,6 +11,9 @@ @JsonIgnore private String password; + @JsonIgnore////// + private String accountColor; + public Account(String aid, String pass) { accountId = aid; password = pass; @@ -25,11 +28,14 @@ } @JsonIgnore public int getBookCount() {return bookcount;} - public void setId(String i) {accountId = i;} + @JsonIgnore public String getId() {return accountId;} public void setPassword(String p) {password = p;} public String getPassword() {return password;} + @JsonIgnore + public void setAccountColor(String a) {accountColor = a;} + public String getAccountColor() {return accountColor;} } diff --git a/src/main/java/org/ntlab/citrusserver/entities/BookModel.java b/src/main/java/org/ntlab/citrusserver/entities/BookModel.java new file mode 100644 index 0000000..b478044 --- /dev/null +++ b/src/main/java/org/ntlab/citrusserver/entities/BookModel.java @@ -0,0 +1,100 @@ +package org.ntlab.citrusserver.entities; + +public class BookModel { + + private int bookId; + private String title; + private boolean publicity; + private String color; + private String accountId; + private String time; + private String accountColor; + private int favoriteCount; + + public BookModel(){} + + public BookModel(int bookId, String title, boolean publicity, String color, String accountId, String time, String accountColor) { + this.bookId = bookId; + this.title = title; + this.publicity = publicity; + this.color = color; + this.accountId = accountId; + this.time = time; + this.accountColor = accountColor; + } + + public BookModel(Book book, String accountId, String accountColor) { + this.bookId = book.getBookId(); + this.title = book.getTitle(); + this.publicity = book.getPublicity(); + this.color = book.getColor(); + this.time = book.getTime(); + this.favoriteCount = book.getFavoritedCount(); + this.accountColor = accountColor; + this.accountId = accountId; + } + + public int getBookId() { + return bookId; + } + + public void setBookId(int bookId) { + this.bookId = bookId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public boolean isPublicity() { + return publicity; + } + + public void setPublicity(boolean publicity) { + this.publicity = publicity; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String getAccountId() { + return accountId; + } + + public void setAccountId(String accountId) { + this.accountId = accountId; + } + + public String getTime() { + return time; + } + + public void setTime(String time) { + this.time = time; + } + + public String getAccountColor() { + return accountColor; + } + + public void setAccountColor(String accountColor) { + this.accountColor = accountColor; + } + + public int getFavoriteCount() { + return favoriteCount; + } + + public void setFavoriteCount(int favoriteCount) { + this.favoriteCount = favoriteCount; + } +} diff --git a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java index 751131f..906bee6 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java @@ -2,6 +2,7 @@ import org.ntlab.citrusserver.entities.Account; import org.ntlab.citrusserver.entities.Book; +import org.ntlab.citrusserver.entities.Schedule; import org.springframework.stereotype.Repository; import java.util.*; @@ -15,6 +16,7 @@ private final List iAccountListeners = new ArrayList<>(); + public AccountManager() { dummyAccount(); } @@ -53,6 +55,7 @@ Account account = new Account(accountId, password); accounts.put(accountId, account); accountToken.put(accountId, token); //accountIDとtokenをHashMapに入れる + accounts.get(accountId).setAccountColor("#D5D5D5"); return token; } else { return null; @@ -82,6 +85,18 @@ } } + // 指定されたIDを変更する (PUT) + public void changeAccountId(String accountId, String newAccountId, String oldPassword, String token) { + if(accountToken.get(accountId).equals(token)) { //token比較 + if(accounts.get(accountId).getPassword().equals(oldPassword)) { //password比較 + + accounts.get(accountId).setId(newAccountId); + accounts.put(newAccountId, accounts.get(accountId)); + accounts.remove(accountId); + } + } + } + // 指定されたIDのパスワードを変更する (PUT) public void changePassword(String accountId, String newPassword, String oldPassword, String token) { if(accountToken.get(accountId).equals(token)) { //token比較 @@ -105,7 +120,22 @@ accounts.get(accountId).setIntroduction(introduction); } } + /////////// + // accountの色を返す(GET) + public String getAccountColor(String accountId){ + if(accounts.containsKey(accountId)) { //アカウントがあるかを確認 + return accounts.get(accountId).getAccountColor(); + } + return null; + } + // accountの色を変更する(PUT) + public void changeAccountColor(String accountId, String newColor, String token){ + if(accountToken.get(accountId).equals(token)) { //token比較 + accounts.get(accountId).setAccountColor(newColor); + } + } + /////////// // アカウントidとパスワードでログインし、tokenを返す (POST) public String login(String accountId, String password) { diff --git a/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java b/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java index d8ca937..0d2b459 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java @@ -94,7 +94,12 @@ //本の公開情報を返す public Boolean getPublicity(String accountId, Integer bookId) { - return booksMap.get(accountId).get(bookId).getPublicity(); + if(booksMap.containsKey(accountId)) { + if(booksMap.get(accountId).containsKey(bookId)) { + return booksMap.get(accountId).get(bookId).getPublicity(); + } + } + return false; } //公開情報を変更する @@ -131,6 +136,18 @@ booksMap.get(accountId).get(bookId).setFavoritedCount(favoriteCount - 1); } + public void changeAccountId(String oldAccountId, String newAccountId) { + if(!booksMap.containsKey(oldAccountId)) { + return; + } + for(Book book : booksMap.get(oldAccountId).values()) { + book.setAccountId(newAccountId); + } + var books = booksMap.get(oldAccountId); + booksMap.put(newAccountId, books); + booksMap.remove(oldAccountId); + } + @Override public void accountDeleted(Account account) { if(!booksMap.containsKey(account.getId())) return; diff --git a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java index fd8d085..f9b07c5 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java @@ -1,18 +1,27 @@ package org.ntlab.citrusserver.repositories; import org.ntlab.citrusserver.entities.Account; +import org.ntlab.citrusserver.entities.Book; +import org.ntlab.citrusserver.entities.BookModel; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; +import java.util.List; @Repository public class FavoriteManager implements IAccountListener { + private final BookManager bookManager; + private final AccountManager accountManager; + @Autowired - public FavoriteManager(AccountManager accountManager) { + public FavoriteManager(AccountManager accountManager, BookManager bookManager) { accountManager.addListener(this); // bookManager.addListener(this); + this.accountManager = accountManager; + this.bookManager = bookManager; } @@ -74,6 +83,19 @@ return favoritesMap.get(accountId); //accountIdをgetすればそれ以降のotherAccountIDとbookIdが返ってくる } + public List getFavoriteList(String accountId) { + if (!favoritesMap.containsKey(accountId)) return null; + List res = new ArrayList<>(); + for(String aid: favoritesMap.get(accountId).keySet()) { + String color = accountManager.getAccountColor(aid); + for(int bid : favoritesMap.get(accountId).get(aid)) { + Book book = bookManager.getBook(aid, bid); + res.add(new BookModel(book, aid, color)); + } + } + return res; + } + //get //取得したotherAccountIdのいいねした本を返す: otherAccountIdがいいねしたAccountIdの本(bookId)の一覧 public HashSet getFavoritesByID(String accountId, String otherAccountId) { //このHashSetはbookId(int型)の集合 diff --git a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java index fdaea51..8128814 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java @@ -6,14 +6,14 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; +import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.LocalDate; +import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import java.util.*; -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; +import java.util.concurrent.ScheduledExecutorService; @Repository public class ScheduleManager implements IAccountListener, IBookListener{ @@ -25,14 +25,9 @@ @Autowired public ScheduleManager(AccountManager accountManager, BookManager bookManager) { this.bookManager = bookManager; + this.bookManager.addListener(this); accountManager.addListener(this); - Calendar c = Calendar.getInstance(); -// this.addSchedule("fish", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DATE), "cinema", c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+" 10:00", c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+" 12:00", 0); -// this.addSchedule("fish", 2024, 5, 28, "test", "2024/05/28 15:00", "2024/05/28 18:00", 1); -// this.addSchedule("bird", c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DATE), "cinema", c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+" 10:00", c.get(Calendar.YEAR)+"/"+(c.get(Calendar.MONTH)+1)+"/"+c.get(Calendar.DATE)+" 12:00", 0); -// this.addSchedule("bird", 2024, 5, 28, "test", "2024/05/28 15:00", "2024/05/28 18:00", 1); - } //管理 @@ -44,20 +39,125 @@ Schedule>>>>> schedules = new HashMap<>(); - public HashMap>>> getScheduleAll(String accountId) { - return schedules.get(accountId); + public HashMap>>> getScheduleAll(String accountId, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + + HashMap>>> resultSchedules + = new HashMap<>(); + + for (int year : accountSchedules.keySet()) { + for (int month : accountSchedules.get(year).keySet()) { + for (int day : accountSchedules.get(year).get(month).keySet()) { + for (int scheduleId : accountSchedules.get(year).get(month).get(day).keySet()) { + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + } + } + } + } + } + return resultSchedules; + } else return schedules.get(accountId); } - public HashMap>> getScheduleYear(String accountId, int year) { - return schedules.get(accountId).get(year); + public HashMap>> getScheduleYear(String accountId, int year, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + HashMap>>> resultSchedules + = new HashMap<>(); + for (int month : accountSchedules.get(year).keySet()) { + for (int day : accountSchedules.get(year).get(month).keySet()) { + for (int scheduleId : accountSchedules.get(year).get(month).get(day).keySet()) { + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + } + } + } + } + return resultSchedules.get(year); + } else return schedules.get(accountId).get(year); } - public HashMap> getScheduleMonth(String accountId, int year, int month) { - return schedules.get(accountId).get(year).get(month); + public HashMap> getScheduleMonth(String accountId, int year, int month, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + HashMap>>> resultSchedules + = new HashMap<>(); + for (int day : accountSchedules.get(year).get(month).keySet()) { + for (int scheduleId : accountSchedules.get(year).get(month).get(day).keySet()) { + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + } + } + } + return resultSchedules.get(year).get(month); + }else return schedules.get(accountId).get(year).get(month); } - public HashMap getScheduleDay(String accountId, int year, int month, int day) { - return schedules.get(accountId).get(year).get(month).get(day); + public HashMap getScheduleDay(String accountId, int year, int month, int day, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + HashMap>>> resultSchedules + = new HashMap<>(); + for (int scheduleId : accountSchedules.get(year).get(month).get(day).keySet()) { + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + }else { + Schedule newSchedule = new Schedule("", schedule.getStartTime(), schedule.getEndTime(),-1,-1); + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, newSchedule); + } + } + return resultSchedules.get(year).get(month).get(day); + }else return schedules.get(accountId).get(year).get(month).get(day); } public Schedule addSchedule(String accountId, int year, int month, int day, String title, String startTime, String endTime, Integer bookId) { @@ -76,60 +176,81 @@ if (!schedules.get(accountId).get(year).containsKey(month)) { schedules.get(accountId).get(year).put(month, new HashMap<>()); } - LocalDate stDate = LocalDate.parse(startTime, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm")); - LocalDate edDate = LocalDate.parse(endTime, DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm")); - int count = getDateRange(stDate, edDate); -// try { -// SimpleDateFormat stDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); -// SimpleDateFormat edDateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm"); -// stDate = stDateFormat.parse(startTime); -// edDate = edDateFormat.parse(endTime); -// }catch (ParseException e){ -// e.printStackTrace(); -// } -// Calendar stcalendar = Calendar.getInstance(); -// Calendar edcalendar = Calendar.getInstance(); -// stcalendar.setTime(stDate); -// edcalendar.setTime(edDate); - if(count > 0){ - for (int i = 0; i <= count; i++) { - if (!schedules.get(accountId).get(year).get(month).containsKey(day+i)) { - schedules.get(accountId).get(year).get(month).put(day+i, new HashMap<>()); - } - String accountSchedule = accountId + year + month + (day+i);// - if (!nextScheduleId.containsKey(accountSchedule)) { - nextScheduleId.put(accountSchedule, 0); - } - int newScheduleId = nextScheduleId.get(accountSchedule); - Schedule newSchedule = new Schedule(title, startTime, endTime, bookId, newScheduleId); - schedules.get(accountId).get(year).get(month).get(day+i).put(newScheduleId, newSchedule); - nextScheduleId.put(accountSchedule, newScheduleId + 1); - if(i==count)return newSchedule; - } - }else { - if (!schedules.get(accountId).get(year).get(month).containsKey(day)) { - schedules.get(accountId).get(year).get(month).put(day, new HashMap<>()); - } - String accountSchedule = accountId + year + month + day;// - if (!nextScheduleId.containsKey(accountSchedule)) { - nextScheduleId.put(accountSchedule, 0); - } - int newScheduleId = nextScheduleId.get(accountSchedule); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm"); + + int newScheduleId = nextScheduleId.computeIfAbsent(accountId, k -> 0); + + LocalDateTime startDateTime = LocalDateTime.parse(startTime, formatter); + LocalDateTime endDateTime = LocalDateTime.parse(endTime, formatter); + + LocalDate startDate = startDateTime.toLocalDate(); + LocalDate endDate = endDateTime.toLocalDate(); + + Schedule res = null; + long days = ChronoUnit.DAYS.between(startDate, endDate) + 1; + + + for(long i = 0; i < days; i++) { + int curYear = startDate.getYear(); + int curMonth = startDate.getMonthValue(); + int curDay = startDate.getDayOfMonth(); + schedules.get(accountId).computeIfAbsent(curYear, k -> new HashMap<>()).computeIfAbsent(curMonth, k -> new HashMap<>()) + .computeIfAbsent(curDay, k -> new HashMap<>()); Schedule newSchedule = new Schedule(title, startTime, endTime, bookId, newScheduleId); - schedules.get(accountId).get(year).get(month).get(day).put(newScheduleId, newSchedule); - nextScheduleId.put(accountSchedule, newScheduleId + 1); - return newSchedule; + schedules.get(accountId).get(curYear).get(curMonth).get(curDay).put(newScheduleId, newSchedule); + nextScheduleId.put(accountId, newScheduleId); + startDate = startDate.plusDays(1); + nextScheduleId.put(accountId, newScheduleId + 1); + res = newSchedule; } - return null; + + return res; } - public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId) { - return schedules.get(accountId).get(year).get(month).get(day).get(scheduleId); + public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId, Integer bookId) { + if (bookId != null) { + // 既存のアカウントのスケジュールを取得 + HashMap>>> accountSchedules = schedules.get(accountId); + HashMap>>> resultSchedules + = new HashMap<>(); + Schedule schedule = accountSchedules.get(year).get(month).get(day).get(scheduleId); + // bookIdが一致するスケジュールをresultSchedulesに追加 + if (schedule.getBookId().equals(bookId)) { + resultSchedules + .computeIfAbsent(year, k -> new HashMap<>()) + .computeIfAbsent(month, k -> new HashMap<>()) + .computeIfAbsent(day, k -> new HashMap<>()) + .put(scheduleId, schedule); + } + return resultSchedules.get(year).get(month).get(day).get(scheduleId); + }else return schedules.get(accountId).get(year).get(month).get(day).get(scheduleId); } public void deleteSchedule(String accountId, int year, int month, int day, int scheduleId) { - schedules.get(accountId).get(year).get(month).get(day).remove(scheduleId); + Schedule schedule = schedules.get(accountId).get(year).get(month).get(day).get(scheduleId); + String startDate = schedule.getStartTime(); + String endDate = schedule.getEndTime(); + String[] startDateTime = startDate.split(" "); + String[] endDateTime = endDate.split(" "); + String[] startYMD = startDateTime[0].split("/"); + String[] endYMD = endDateTime[0].split("/"); + LocalDate start = LocalDate.of(Integer.parseInt(startYMD[0]), Integer.parseInt(startYMD[1]), Integer.parseInt(startYMD[2])); + LocalDate end = LocalDate.of(Integer.parseInt(endYMD[0]), Integer.parseInt(endYMD[1]), Integer.parseInt(endYMD[2])); + long dist = ChronoUnit.DAYS.between(start, end); + LocalDate now = LocalDate.of(year, month, day); + long toStart = ChronoUnit.DAYS.between(now, start); + for(int i = 0; i < dist + 1; i++) { + int nowYear = start.getYear(); + int nowMonth = start.getMonthValue(); + int nowDay = start.getDayOfMonth(); + schedules.get(accountId).get(nowYear).get(nowMonth).get(nowDay).remove(schedule.getScheduleId()); + start = start.plusDays(1); + } } public void setScheduleStartTime(String accountId, int year, int month, int day, int scheduleId, String newTime) { @@ -174,6 +295,15 @@ } } + public void changeAccountId(String oldAccountId, String newAccountId) { + if(!schedules.containsKey(oldAccountId)){ + return; + } + var oldSchedules = schedules.get(oldAccountId); + schedules.put(newAccountId, oldSchedules); + schedules.remove(oldAccountId); + } + @Override public void bookChanged(Account account, Book book) { diff --git a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java index 4595917..0d62fab 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java @@ -338,7 +338,8 @@ return 1; } - public int setTodo(String accountId, int bookId, int year, int month, int day, int todoId, String title){ + public int setTodo(String accountId, int bookId, int year, int month, int day, + int newBookId, int newYear, int newMonth, int newDay, int todoId, String title){ if(!todos.containsKey(accountId)){ return -1; } @@ -357,11 +358,8 @@ if(!todos.get(accountId).get(bookId).get(year).get(month).get(day).containsKey(todoId)){ return -1; } - todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setTitle(title); - todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setBookId(bookId); - todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setYear(year); - todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setMonth(month); - todos.get(accountId).get(bookId).get(year).get(month).get(day).get(todoId).setDay(day); + todos.get(accountId).get(bookId).get(year).get(month).get(day).remove(todoId); + createTodo(accountId, newBookId, newYear, newMonth, newDay, title); return 1; } @@ -392,6 +390,15 @@ todos.remove(accountId); } + public void changeAccountId(String oldAccountId, String newAccountId) { + if(todos.containsKey(oldAccountId)) { + return; + } + var oldTodos = todos.get(oldAccountId); + todos.put(newAccountId, oldTodos); + todos.remove(oldAccountId); + } + @Override public void accountDeleted(Account account) { deleteAllTodosByAccountId(account.getId()); diff --git a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java index 2a74ab0..edc0e1a 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java @@ -16,11 +16,17 @@ @Component //accountRestのインスタンスを一個作る public class AccountsRest { - private final AccountManager accountManager; //finalは書き換えられない + private final AccountManager accountManager; + private final BookManager bookManager; + private final ScheduleManager scheduleManager; + private final TodoManager todoManager;//finalは書き換えられない @Autowired//springbootの決まり - public AccountsRest(AccountManager am) { + public AccountsRest(AccountManager am, BookManager bm, ScheduleManager sm, TodoManager tm) { + bookManager = bm; + scheduleManager = sm; accountManager = am; + todoManager = tm; } // アカウントの一覧をリストとして返す(GET) @@ -84,6 +90,39 @@ } + //accountのidを変更する(PUT) + @Path("/{account_id}") + @PUT + @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 + public void changeAccountId(@PathParam("account_id") String accountId, + @FormParam("new_account_id")String newAccountId, + @FormParam("old_password")String oldPassword, + @FormParam("token") String token){ + //404 + if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 + var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 + throw new WebApplicationException(response.build()); + } + + if(!accountManager.getAccount(accountId).getPassword().equals(oldPassword)) { + var response = Response.status(Response.Status.BAD_REQUEST).entity("パスワードが違います");//404 + throw new WebApplicationException(response.build()); + } + + //成功 + if(accountManager.checkToken(accountId, token)) { + accountManager.changeAccountId(accountId, newAccountId,oldPassword, token); + bookManager.changeAccountId(accountId, newAccountId); + scheduleManager.changeAccountId(accountId, newAccountId); + todoManager.changeAccountId(accountId, newAccountId); + return; + } + + //403 + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403 + throw new WebApplicationException(response.build()); + } + //指定されたIDのパスワードを変更する (PUT) @Path("/{account_id}/password") @PUT @@ -92,6 +131,10 @@ @FormParam("new_password")String newPassword, @FormParam("old_password")String oldPassword, @FormParam("token") String token){ + if(!accountManager.getAccount(accountId).getPassword().equals(oldPassword)) { + var response = Response.status(Response.Status.BAD_REQUEST).entity("パスワードが違います");//404 + throw new WebApplicationException(response.build()); + } if(accountManager.checkToken(accountId, token)) { accountManager.changePassword(accountId, newPassword, oldPassword, token); return; @@ -136,27 +179,42 @@ var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403 throw new WebApplicationException(response.build()); } -///////// -// @Path("/{account_id}/photo") -// //画像を返す -// @GET -// public String getAccount(@PathParam("account_id") String accountId){ -// Account ac = accountManager.getAccount(accountId); -// return ac.getPhoto(); -// } -// @Path("/{account_id}/photo") -// @PUT - -////////// -// お気に入りの本のbook_idを削除する (DELETE) -// @Path("/{account_id}/favorites/{other_account_id}/{book_id}") -// @DELETE - - // いいねした本のアカウントIDとbook_idを追加する(いいねした側に追加) (PUT) - //@Path("/{account_id}/favorites/{other_account_id}/{book_id}") -// @PUT + ///////////(9/26) + // accountの色を返す(GET) + @Path("/{account_id}/accountColor") + @GET + @Produces(MediaType.APPLICATION_JSON) + public String getAccountColor(@PathParam("account_id") String accountId){ + String accountColor = accountManager.getAccountColor(accountId); + return "\"" + accountColor + "\""; + } + + //accountの色を変更する(PUT) + @Path("/{account_id}/accountColor") + @PUT + @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 + public void changeAccountColor(@PathParam("account_id") String accountId, + @FormParam("accountColor")String accountColor, + @FormParam("token") String token){ + //404 + if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 + var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 + throw new WebApplicationException(response.build()); + } + //成功 + if(accountManager.checkToken(accountId, token)) { + //accountManager.changeAccountColor(accountId, "D5D5D5", token); + accountManager.changeAccountColor(accountId, accountColor, token); + return; + } + + //403 + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403 + throw new WebApplicationException(response.build()); + } + /////////////////// // アカウントidとパスワードでログインし、tokenを返す (POST) @Path("/{account_id}/login") diff --git a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java index 5dcacc7..efd8968 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java @@ -147,24 +147,24 @@ return "success"; } - @Path("/{account_id}/books/{book_id}/favoriteCount") + @Path("/{account_id}/books/{book_id}/registerFavoriteCount") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String registerFavoriteCount(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id){ accountCheck(account_id); bookManager.registerFavoriteCount(account_id, book_id); - return "success"; + return "\"success\""; } - @Path("/{account_id}/books/{book_id}/favoriteCount") + @Path("/{account_id}/books/{book_id}/unregisterFavoriteCount") @PUT @Consumes(MediaType.APPLICATION_FORM_URLENCODED) public String unregisterFavoriteCount(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id){ accountCheck(account_id); bookManager.unregisterFavoriteCount(account_id, book_id); - return "success"; + return "\"success\""; } ///--------------------------------------------------------------------- diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java index 9a96b4e..d7c0d27 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java @@ -31,7 +31,7 @@ @GET @Produces(MediaType.APPLICATION_JSON) public HashSet getFavorited(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ - if (accountManager.checkToken(account_id, token)) { + if (accountManager.checkToken(account_id, token) || bookManager.getPublicity(account_id, book_id)) { return favoriteManager.getFavorited(account_id, book_id); } return null; diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java index 43d30db..d496895 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritesRest.java @@ -4,13 +4,18 @@ import javax.ws.rs.*; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; + +import org.ntlab.citrusserver.entities.Book; +import org.ntlab.citrusserver.entities.BookModel; import org.ntlab.citrusserver.repositories.AccountManager; +import org.ntlab.citrusserver.repositories.BookManager; import org.ntlab.citrusserver.repositories.FavoriteManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.HashSet; +import java.util.List; import java.util.Set; @Path("/accounts") @@ -19,29 +24,29 @@ public class FavoritesRest { private final FavoriteManager favoriteManager; private final AccountManager accountManager; + private final BookManager bookManager; @Autowired - public FavoritesRest(FavoriteManager fm, AccountManager am){ + public FavoritesRest(FavoriteManager fm, AccountManager am, BookManager bm){ favoriteManager = fm; accountManager = am; + bookManager = bm; } @Path("/{account_id}/favorites") @GET @Produces(MediaType.APPLICATION_JSON) - public HashMap> getFavoritesBooks(@PathParam("account_id") String accountId, @QueryParam("token") String token){ - if(favoriteManager.getFavorites(accountId) == null){ + public List getFavoritesBooks(@PathParam("account_id") String myAccountId, @QueryParam("token") String token){ + if(favoriteManager.getFavorites(myAccountId) == null){ var response = Response.status(Response.Status.NOT_FOUND).entity("アカウントが見つかりません"); throw new WebApplicationException(response.build()); } else{ - if(!accountManager.checkToken(accountId, token)) { + if(!accountManager.checkToken(myAccountId, token)) { var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); throw new WebApplicationException(response.build()); } - else{ - return favoriteManager.getFavorites(accountId); - } + return favoriteManager.getFavoriteList(myAccountId); } } diff --git a/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java index 4820154..4782425 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/PublicBooksRest.java @@ -8,6 +8,7 @@ import org.springframework.stereotype.Component; import java.util.ArrayList; +import java.util.Objects; @Path("/public_books") @Component @@ -34,21 +35,21 @@ public ArrayList searchBooksByTitleAndAccount(@QueryParam("search_title") String search_title, @QueryParam("search_account_id") String search_account_id, @QueryParam("sort_by") Integer sort_by) { if(sort_by == null) { //ソートしない場合 - if (search_title != null && search_account_id != null) { //タイトルとアカウントIDでの検索 + if (!Objects.equals(search_title, "") && !Objects.equals(search_account_id, "")) { //タイトルとアカウントIDでの検索 return publicBookManager.searchBooksByTitleAndAccount(search_title, search_account_id); - } else if (search_title != null) { //タイトルのみでの検索 + } else if (!Objects.equals(search_title, "")) { //タイトルのみでの検索 return publicBookManager.searchBooksByTitle(search_title); - } else if (search_account_id != null) { //アカウントIDのみでの検索 + } else if (!Objects.equals(search_account_id, "")) { //アカウントIDのみでの検索 return publicBookManager.searchBooksByAccount(search_account_id); } else { //タイトルもアカウントIDもない場合(すべての本を返す) return publicBookManager.getAllPublicBooks(); } } else { //ソートする場合 - if(search_title != null && search_account_id != null) { //タイトルとアカウントIDでの検索 + if(!Objects.equals(search_title, "") && !Objects.equals(search_account_id, "")) { //タイトルとアカウントIDでの検索 return publicBookManager.searchBooksByTitleAndAccount(search_title, search_account_id, sort_by); - } else if(search_title != null) { //タイトルのみでの検索 + } else if(!Objects.equals(search_title, "")) { //タイトルのみでの検索 return publicBookManager.searchBooksByTitle(search_title, sort_by); - } else if(search_account_id != null) { //アカウントIDのみでの検索 + } else if(!Objects.equals(search_account_id, "")) { //アカウントIDのみでの検索 return publicBookManager.searchBooksByAccount(search_account_id, sort_by); } else { //タイトルもアカウントIDもない場合(すべての本を返す) return publicBookManager.getAllPublicBooks(sort_by); diff --git a/src/main/java/org/ntlab/citrusserver/resources/ScheduleRest.java b/src/main/java/org/ntlab/citrusserver/resources/ScheduleRest.java index f880c8a..77d4341 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/ScheduleRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/ScheduleRest.java @@ -3,6 +3,7 @@ import javax.ws.rs.core.Response; import org.ntlab.citrusserver.entities.Schedule; import org.ntlab.citrusserver.repositories.AccountManager; +import org.ntlab.citrusserver.repositories.BookManager; import org.ntlab.citrusserver.repositories.ScheduleManager; import javax.ws.rs.*; import javax.ws.rs.core.MediaType; @@ -18,20 +19,23 @@ public class ScheduleRest { private final ScheduleManager scheduleManager; private final AccountManager accountManager; + private final BookManager bookManager; @Autowired - public ScheduleRest(ScheduleManager sm, AccountManager ac){ + public ScheduleRest(ScheduleManager sm, AccountManager ac, BookManager bookManager){ scheduleManager = sm; accountManager = ac; + this.bookManager = bookManager; } @Path("/{account_id}/schedule") @GET//登録されたスケジュールのidを返す @Produces(MediaType.APPLICATION_JSON) public HashMap>>> getScheduleAll(@PathParam("account_id") String accountId, - @QueryParam("token") String token){ - if(accountManager.checkToken(accountId,token)) { - return scheduleManager.getScheduleAll(accountId); + @QueryParam("token") String token, + @QueryParam("book_id") Integer bookId){ + if(accountManager.checkToken(accountId,token)||bookManager.getPublicity(accountId, bookId)) { + return scheduleManager.getScheduleAll(accountId, bookId); } var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); throw new WebApplicationException(response.build()); @@ -41,9 +45,10 @@ @GET//登録されたスケジュールのidを年単位で返す @Produces(MediaType.APPLICATION_JSON) public HashMap>> getScheduleYear(@PathParam("account_id") String accountId, - @PathParam("year") Integer year,@QueryParam("token") String token){ - if(accountManager.checkToken(accountId,token)) { - return scheduleManager.getScheduleYear(accountId,year); + @PathParam("year") Integer year, + @QueryParam("token") String token, @QueryParam("book_id") Integer bookId){ + if(accountManager.checkToken(accountId,token)||bookManager.getPublicity(accountId, bookId)) { + return scheduleManager.getScheduleYear(accountId,year, bookId); } var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); throw new WebApplicationException(response.build()); @@ -54,9 +59,9 @@ @Produces(MediaType.APPLICATION_JSON) public HashMap> getScheduleMonth(@PathParam("account_id") String accountId, @PathParam("year") Integer year,@PathParam("month") Integer month, - @QueryParam("token") String token){ - if(accountManager.checkToken(accountId,token)) { - return scheduleManager.getScheduleMonth(accountId,year,month); + @QueryParam("token") String token, @QueryParam("book_id") Integer bookId){ + if(accountManager.checkToken(accountId,token)||bookManager.getPublicity(accountId, bookId)) { + return scheduleManager.getScheduleMonth(accountId,year,month, bookId); } var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); throw new WebApplicationException(response.build()); @@ -67,9 +72,9 @@ @Produces(MediaType.APPLICATION_JSON) public HashMap getScheduleDay(@PathParam("account_id") String accountId, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, - @QueryParam("token") String token){ - if(accountManager.checkToken(accountId,token)) { - return scheduleManager.getScheduleDay(accountId,year,month,day); + @QueryParam("token") String token, @QueryParam("book_id") Integer bookId){ + if(accountManager.checkToken(accountId,token)||bookManager.getPublicity(accountId, bookId)) { + return scheduleManager.getScheduleDay(accountId,year,month,day, bookId); } var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); throw new WebApplicationException(response.build()); @@ -103,9 +108,9 @@ @Produces(MediaType.APPLICATION_JSON) public Schedule getScheduleInfo(@PathParam("account_id") String accountId, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, - @PathParam("schedule_id") Integer scheduleId, @QueryParam("token") String token) { + @PathParam("schedule_id") Integer scheduleId, @QueryParam("token") String token, @QueryParam("book_id") Integer bookId) { if(accountManager.checkToken(accountId,token)) { - Schedule schedule = scheduleManager.getScheduleId(accountId, year, month, day, scheduleId); + Schedule schedule = scheduleManager.getScheduleId(accountId, year, month, day, scheduleId, bookId); return schedule; }else { var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); diff --git a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java index b435fc2..2ef7939 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java @@ -119,10 +119,10 @@ @Consumes(MediaType.APPLICATION_FORM_URLENCODED) @Produces(MediaType.APPLICATION_JSON) public void setTodo(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, - @PathParam("month") Integer month, @PathParam("day") Integer day, @PathParam("todo_id") Integer todo_id, @FormParam("title") String title, + @PathParam("month") Integer month, @PathParam("day") Integer day, @PathParam("todo_id") Integer todo_id, @FormParam("title") String title, @FormParam("new_book_id") Integer newBookId, @FormParam("new_year") Integer new_year,@FormParam("new_month") Integer new_month,@FormParam("new_day") Integer new_day, @FormParam("token") String token){ if (accountManager.checkToken(account_id, token)) { - if (todoManager.setTodo(account_id, book_id, new_year, new_month, new_day, todo_id, title) == -1) { + if (todoManager.setTodo(account_id, book_id, year, month, day, newBookId, new_year, new_month, new_day, todo_id, title) == -1) { var response = Response.status(Response.Status.BAD_REQUEST).entity("変更失敗"); throw new WebApplicationException(response.build()); }