diff --git a/src/main/java/org/ntlab/citrusserver/entities/Book.java b/src/main/java/org/ntlab/citrusserver/entities/Book.java index c68c40e..08a7d4b 100644 --- a/src/main/java/org/ntlab/citrusserver/entities/Book.java +++ b/src/main/java/org/ntlab/citrusserver/entities/Book.java @@ -8,6 +8,7 @@ private String color; private String accountId; private String time; + private int favoriteCount; public Book(String accountId, Integer bookId, String title, boolean publicity, String color, String time) { @@ -19,6 +20,7 @@ this.color = color; this.time = time; + this.favoriteCount = 0; } @@ -34,4 +36,6 @@ public String getAccountId() {return accountId;} public void setTime(String t) {time = t;} public String getTime() {return time;} + public void setFavoritedCount(int f) {favoriteCount = f;} + public int getFavoritedCount() {return favoriteCount;} } diff --git a/src/main/java/org/ntlab/citrusserver/entities/Todo.java b/src/main/java/org/ntlab/citrusserver/entities/Todo.java index 8ba929b..6a19715 100644 --- a/src/main/java/org/ntlab/citrusserver/entities/Todo.java +++ b/src/main/java/org/ntlab/citrusserver/entities/Todo.java @@ -7,27 +7,35 @@ int month; int day; Integer todoId; + Integer bookId; - public Todo(String t, boolean c, int y, int m, int d, Integer tid) { - title = t; - check = c; - year = y; - month = m; - day = d; - todoId = tid; + public Todo(String title, boolean check, int year, int month, int day, Integer tid, Integer bid) { + this.title = title; + this.check = check; + this.year = year; + this.month = month; + this.day = day; + this.todoId = tid; + this.bookId = bid; } + //setter public void setTitle(String t) {title = t;} - public String getTitle() {return title;} public void setCheck(boolean c) {check = c;} - public boolean getCheck() {return check;} public void setYear(int y) {year = y;} - public int getYear() {return year;} public void setMonth(int m) {month = m;} - public int getMonth() {return month;} public void setDay(int d) {day = d;} - public int getDay() {return day;} public void setTodoId(Integer t) {todoId = t;} - public Integer getTodoId() { - return todoId; - } + public void setBookId(Integer b) {bookId = b;} + //getter + public String getTitle() {return title;} + public boolean getCheck() {return check;} + public int getYear() {return year;} + public int getMonth() {return month;} + public int getDay() {return day;} + public Integer getTodoId() {return todoId;} + public Integer getBookId() {return bookId;} + + + + } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java index 3580f3c..751131f 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/AccountManager.java @@ -1,6 +1,7 @@ package org.ntlab.citrusserver.repositories; import org.ntlab.citrusserver.entities.Account; +import org.ntlab.citrusserver.entities.Book; import org.springframework.stereotype.Repository; import java.util.*; @@ -12,10 +13,17 @@ private final HashMap accountToken = new HashMap<>(); //keyがaccountId,valueがtoken + private final List iAccountListeners = new ArrayList<>(); + public AccountManager() { dummyAccount(); } + public void addListener(IAccountListener iAccountListener) + { + iAccountListeners.add(iAccountListener); + } + public void dummyAccount() { //ダミーアカウントの作成 String accountId = "fish"; String password = "abc"; @@ -67,7 +75,8 @@ // アカウント情報を全削除する(DELETE) public void deleteAccount(String accountId, String token, String password) { if(accountToken.get(accountId).equals(token)) { //token比較 - if(accounts.get(accountId).getPassword().equals(password)) { //password比較 + if(accounts.get(accountId).getPassword().equals(password)) {//password比較 + notifyDeletedListener(getAccount(accountId)); accounts.remove(accountId); } } @@ -84,7 +93,10 @@ // 指定されたIDの自己紹介を返す(GET) public String AccountIntro(String accountId) { - return accounts.get(accountId).getIntroduction(); + if(accounts.containsKey(accountId)) { //アカウントがあるかを確認 + return accounts.get(accountId).getIntroduction(); + } + return null; } // 指定されたIDの自己紹介を変更する (PUT) @@ -106,4 +118,12 @@ return null; } + private void notifyDeletedListener(Account account) + { + for(IAccountListener iAccountListener : iAccountListeners) + { + iAccountListener.accountDeleted(account); + } + } + } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java b/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java index 7bbe584..d8ca937 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/BookManager.java @@ -14,7 +14,7 @@ import java.util.Objects; @Repository -public class BookManager { +public class BookManager implements IAccountListener { private final HashMap> booksMap = new HashMap<>(); private final AccountManager accountManager; //仮 @@ -24,6 +24,7 @@ public BookManager(AccountManager accountManager) { this.accountManager = accountManager; + accountManager.addListener(this); createBook("fish","Aさんのダミ本","#B2A9F0", false);//ダミーの本 createBook("bird","Bさんのダミ本","#C1F8E5", false);//ダミーの本 } @@ -120,6 +121,26 @@ booksMap.get(accountId).get(bookId).setColor(color); } + public void registerFavoriteCount(String accountId, Integer bookId){ + int favoriteCount = booksMap.get(accountId).get(bookId).getFavoritedCount(); + booksMap.get(accountId).get(bookId).setFavoritedCount(favoriteCount + 1); + } + + public void unregisterFavoriteCount(String accountId, Integer bookId){ + int favoriteCount = booksMap.get(accountId).get(bookId).getFavoritedCount(); + booksMap.get(accountId).get(bookId).setFavoritedCount(favoriteCount - 1); + } + + @Override + public void accountDeleted(Account account) { + if(!booksMap.containsKey(account.getId())) return; + for(Book book : booksMap.get(account.getId()).values()){ + notifyDeletedListener(account, book); + + } + deleteAllBooks(account.getId()); + } + //--------------------------------------------------------------------------------- // private //--------------------------------------------------------------------------------- @@ -150,4 +171,6 @@ return dtf1.format(nowDate); } + + } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java index 0a87c00..fd8d085 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/FavoriteManager.java @@ -1,4 +1,5 @@ package org.ntlab.citrusserver.repositories; +import org.ntlab.citrusserver.entities.Account; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; @@ -6,12 +7,13 @@ import java.util.HashSet; @Repository -public class FavoriteManager { - private final AccountManager accountManager; +public class FavoriteManager implements IAccountListener { @Autowired public FavoriteManager(AccountManager accountManager) { - this.accountManager = accountManager; + accountManager.addListener(this); +// bookManager.addListener(this); + } //favorited @@ -38,8 +40,8 @@ //get //本をいいねしたアカウントをリストとして返す: accountIdの本(bookId)にいいねした人(otherAccount)を返す public HashSet getFavorited(String accountId, int bookId) { //このHashSetはotherAccountId(string)の集合 - if (accountManager.getAccount(accountId) == null) return null; //アカウントが存在しない - if (favoritedMap.get(accountId).get(bookId) == null) return null; //bookIdが存在しない + if (!favoritedMap.containsKey(accountId)) return null; //アカウントが存在しない + if (!favoritedMap.get(accountId).containsKey(bookId)) return null; //bookIdが存在しない return favoritedMap.get(accountId).get(bookId);//accountIdをgetして、bookIdまでgetしたら次otherAccount } @@ -65,19 +67,17 @@ } - //favorites - //get //いいねした人の一覧を返す: accountIdの本(bookId)にいいねしたotherAccountIdとその本の一覧 public HashMap> getFavorites(String accountId) { //otherAccountIDといいねしたbookIdの集合(本はaccountIdのもの限定) - if (accountManager.getAccount(accountId) == null) return null; + if (!favoritesMap.containsKey(accountId)) return null; return favoritesMap.get(accountId); //accountIdをgetすればそれ以降のotherAccountIDとbookIdが返ってくる } //get //取得したotherAccountIdのいいねした本を返す: otherAccountIdがいいねしたAccountIdの本(bookId)の一覧 public HashSet getFavoritesByID(String accountId, String otherAccountId) { //このHashSetはbookId(int型)の集合 - if (accountManager.getAccount(accountId) == null) return null; //accountIdがなかったらnull + if (!favoritesMap.containsKey(accountId)) return null; //accountIdがなかったらnull if (!favoritesMap.get(accountId).containsKey(otherAccountId)) return null; // //otherAccountIdがなかったらnull return favoritesMap.get(accountId).get(otherAccountId);//otherAccountIdまでgetすればbookIdの集合が得られる @@ -103,28 +103,60 @@ } - // accountを削除したいとき +// accountを削除したいとき public void removeFavoriteById(String accountId) { // accountIdはamika1107 - favoritedMap.remove(accountId); // tedのアカウントから下全部消す - for (String otherAccountId : favoritesMap.get(accountId).keySet()) { - favoritesMap.get(accountId).remove(otherAccountId); // tesのotherから下全部 + //birdのアカウントを消した時 + if (favoritedMap.containsKey(accountId)) { + for (Integer bookId : favoritedMap.get(accountId).keySet()) { + for (String fanAccountId : favoritedMap.get(accountId).get(bookId)) { + favoritesMap.get(fanAccountId).remove(accountId); // tesのotherから下全部 + } + } + favoritedMap.remove(accountId); // tedのアカウントから下全部消す } - for (Integer bookId : favoritedMap.get(accountId).keySet()) { - for (String otherAccountId : favoritedMap.get(accountId).get(bookId)) { - favoritedMap.get(accountId).get(bookId).remove(otherAccountId); + if(favoritesMap.containsKey(accountId)) { + for (String starAccountId : favoritesMap.get(accountId).keySet()) { + for (Integer bookId : favoritesMap.get(accountId).get(starAccountId)) { + favoritedMap.get(starAccountId).get(bookId).remove(accountId); + } } + favoritesMap.remove(accountId); } - favoritesMap.remove(accountId); } // bookIdを削除したいとき public void removeFavoriteByBookID(String accountId, Integer bookId) { // accountIdはamika1107 - for (String otherAccountId : favoritedMap.get(accountId).get(bookId)){ - favoritesMap.get(otherAccountId).get(accountId).remove(bookId); // tesのbookId を削除 + if(favoritedMap.containsKey(accountId) && favoritedMap.get(accountId).containsKey(bookId)) { + for (String otherAccountId : favoritedMap.get(accountId).get(bookId)){ + favoritesMap.get(otherAccountId).get(accountId).remove(bookId); + if(favoritesMap.get(otherAccountId).get(accountId).isEmpty()) { + favoritesMap.get(otherAccountId).remove(accountId); + }// tesのbookId を削除 + } + favoritedMap.get(accountId).remove(bookId); // tedのbookIdを消したいとき } - favoritedMap.get(accountId).remove(bookId); // tedのbookIdを消したいとき + + } + + @Override + public void accountDeleted(Account account) { + removeFavoriteById(account.getId()); + } + + +// @Override +// public void bookChanged(Account account, Book book) { +// +// } +// +// @Override +// public void bookDeleted(Account account, Book book) { +// removeFavoriteByBookID(account.getId(), book.getBookId()); +// } + + } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/IAccountListener.java b/src/main/java/org/ntlab/citrusserver/repositories/IAccountListener.java new file mode 100644 index 0000000..4d0dd58 --- /dev/null +++ b/src/main/java/org/ntlab/citrusserver/repositories/IAccountListener.java @@ -0,0 +1,7 @@ +package org.ntlab.citrusserver.repositories; + +import org.ntlab.citrusserver.entities.Account; + +public interface IAccountListener { + void accountDeleted(Account account); +} diff --git a/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java b/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java index bde074a..3bc8d98 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/PublicBookManager.java @@ -111,9 +111,17 @@ books.sort(Comparator.comparing(Book::getTime).reversed()); return books; case 1: + books.sort(Comparator.comparing(Book::getTime)); + return books; + case 2: ArrayList booksForSort = new ArrayList<>(); for(Book book : books){ - booksForSort.add(new BookForSort(book, favoriteManager.getFavorited(book.getAccountId(), book.getBookId()).size())); + HashSet favorites = favoriteManager.getFavorited(book.getAccountId(), book.getBookId()); + int count = 0; + if(favorites != null){ + count = favorites.size(); + } + booksForSort.add(new BookForSort(book, count)); } booksForSort.sort(Comparator.comparing(BookForSort::favoriteNum).reversed()); ArrayList res = new ArrayList<>(); @@ -122,7 +130,7 @@ } return res; } - return new ArrayList<>(); + return books; } } diff --git a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java index 7f6718d..fdaea51 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/ScheduleManager.java @@ -6,23 +6,32 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; -import java.util.HashMap; +import java.text.SimpleDateFormat; +import java.time.LocalDate; +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; @Repository -public class ScheduleManager implements IBookListener { +public class ScheduleManager implements IAccountListener, IBookListener{ private final BookManager bookManager; private final HashMap nextScheduleId = new HashMap<>(); @Autowired - public ScheduleManager(BookManager bookManager) { + public ScheduleManager(AccountManager accountManager, BookManager bookManager) { this.bookManager = bookManager; this.bookManager.addListener(this); - this.addSchedule("fish", 2024, 5, 28, "cinema", "10;00", "12:00", 0); - this.addSchedule("fish", 2024, 5, 28, "test", "15;00", "18:00", 1); - this.addSchedule("bird", 2024, 5, 28, "cinema", "10;00", "12:00", 0); - this.addSchedule("bird", 2024, 5, 28, "test", "15;00", "18:00", 1); + 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); } @@ -67,20 +76,52 @@ if (!schedules.get(accountId).get(year).containsKey(month)) { schedules.get(accountId).get(year).put(month, new HashMap<>()); } - if (!schedules.get(accountId).get(year).get(month).containsKey(day)) { - schedules.get(accountId).get(year).get(month).put(day, 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); - String accountSchedule = accountId + year + month + day;// - if (!nextScheduleId.containsKey(accountSchedule)) { - nextScheduleId.put(accountSchedule, 0); + 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); + 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; } - - int newScheduleId = nextScheduleId.get(accountSchedule); - 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; + return null; } public Schedule getScheduleId(String accountId, int year, int month, int day, int scheduleId) { @@ -155,4 +196,24 @@ } } } + + @Override + public void accountDeleted(Account account) { + deleteSchedules(account.getId()); + } + + public static int getDateRange(LocalDate startDate, LocalDate endDate){ + List dateList = new ArrayList<>(); + LocalDate currentDate = startDate; + int count = 0; + + while (!currentDate.isAfter(endDate)) { + dateList.add(currentDate); + currentDate = currentDate.plusDays(1); + count++; + } + + return count-1; + } } + diff --git a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java index db1a1df..4595917 100644 --- a/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java +++ b/src/main/java/org/ntlab/citrusserver/repositories/TodoManager.java @@ -1,13 +1,17 @@ package org.ntlab.citrusserver.repositories; +import org.ntlab.citrusserver.entities.Account; +import org.ntlab.citrusserver.entities.Book; import org.ntlab.citrusserver.entities.Todo; import org.springframework.stereotype.Repository; +import java.util.Calendar; +import java.util.Collections; import java.util.HashMap; @Repository -public class TodoManager { +public class TodoManager implements IAccountListener, IBookListener { /** * todoをすべて管理します @@ -27,11 +31,14 @@ private final BookManager bookManager; - public TodoManager(BookManager bookManager) { + public TodoManager(AccountManager accountManager, BookManager bookManager) { this.bookManager = bookManager; + accountManager.addListener(this); + this.bookManager.addListener(this); String[][] accounts = {{"fish", "abc", "def"}, {"bird", "abc", "xyz"}}; - createTodo(accounts[0][0], 1, 2024, 5, 28, "ダミーtodo1"); - createTodo(accounts[1][0], 1, 2024, 5, 28, "ダミーtodo2"); + Calendar c = Calendar.getInstance(); + createTodo(accounts[0][0], 1, c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DATE), "ダミーtodo1"); + createTodo(accounts[1][0], 1, c.get(Calendar.YEAR), c.get(Calendar.MONTH) + 1, c.get(Calendar.DATE), "ダミーtodo2"); } @@ -229,7 +236,7 @@ } int newTodoId = nextTodoId.get(accountBook); - Todo newTodo = new Todo(title, false, year, month, day, newTodoId); + Todo newTodo = new Todo(title, false, year, month, day, newTodoId, bookId); todos.get(accountId).get(bookId).get(year).get(month).get(day).put(newTodoId, newTodo); nextTodoId.put(accountBook, newTodoId + 1); return newTodo; @@ -331,6 +338,33 @@ return 1; } + public int setTodo(String accountId, int bookId, int year, int month, int day, int todoId, String title){ + if(!todos.containsKey(accountId)){ + return -1; + } + if(!todos.get(accountId).containsKey(bookId)){ + return -1; + } + if(!todos.get(accountId).get(bookId).containsKey(year)){ + return -1; + } + if(!todos.get(accountId).get(bookId).get(year).containsKey(month)){ + return -1; + } + if(!todos.get(accountId).get(bookId).get(year).get(month).containsKey(day)){ + return -1; + } + 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); + return 1; + } + /** *todo_idを指定してtodoの達成状態を変更 * @@ -358,4 +392,18 @@ todos.remove(accountId); } + @Override + public void accountDeleted(Account account) { + deleteAllTodosByAccountId(account.getId()); + } + + @Override + public void bookChanged(Account account, Book book) { + + } + @Override + public void bookDeleted(Account accountIn, Book book) { + deleteAllTodosByBookId(accountIn.getId(), book.getBookId()); + } + } diff --git a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java index 6da636e..2a74ab0 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/AccountsRest.java @@ -14,20 +14,13 @@ @Path("/accounts") @Component //accountRestのインスタンスを一個作る + public class AccountsRest { private final AccountManager accountManager; //finalは書き換えられない - private final BookManager bookManager; - private final TodoManager todoManager; - private final ScheduleManager scheduleManager; - private final FavoriteManager favoriteManager; @Autowired//springbootの決まり - public AccountsRest(AccountManager am, BookManager bm, TodoManager tm, ScheduleManager sm, FavoriteManager favoriteManager) { + public AccountsRest(AccountManager am) { accountManager = am; - bookManager = bm; - todoManager = tm; - scheduleManager = sm; - this.favoriteManager = favoriteManager; } // アカウントの一覧をリストとして返す(GET) @@ -42,6 +35,7 @@ // account_idとpasswordを設定し新しいアカウントを作成する(POST) @POST @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 + @Produces(MediaType.APPLICATION_JSON) public String signup(@FormParam("account_id") String accountId, @FormParam("password") String password) { String token; if (password == null) { @@ -53,7 +47,7 @@ var response = Response.status(Response.Status.CONFLICT).entity("id '" + accountId + "' は既に存在します");//404 throw new WebApplicationException(response.build()); } - return token; + return "\"" + token +"\""; } // 指定されたアカウントの情報を返す(GET) @@ -76,10 +70,6 @@ @QueryParam("token") String token, @QueryParam("password")String password) { if(accountManager.checkToken(accountId, token)) { - bookManager.deleteAllBooks(accountId); - todoManager.deleteAllTodosByAccountId(accountId); - scheduleManager.deleteSchedules(accountId); - favoriteManager.removeFavoriteById(accountId); accountManager.deleteAccount(accountId, token, password); return; } @@ -94,10 +84,10 @@ } - //指定されたIDのパスワードを変更する (PUT) @Path("/{account_id}/password") @PUT + @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 public void changePassword(@PathParam("account_id") String accountId, @FormParam("new_password")String newPassword, @FormParam("old_password")String oldPassword, @@ -129,6 +119,7 @@ // 指定されたIDの自己紹介を変更する (PUT) @Path("/{account_id}/introduction") @PUT + @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 public void changeIntroduction(@PathParam("account_id") String accountId, @FormParam("introduction")String introduction, @FormParam("token") String token){ @@ -170,13 +161,15 @@ // アカウントidとパスワードでログインし、tokenを返す (POST) @Path("/{account_id}/login") @POST + @Produces(MediaType.APPLICATION_JSON) + @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時 public String login(@PathParam("account_id") String accountId,@FormParam("password") String password) { //404 if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時 var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404 throw new WebApplicationException(response.build()); } - return accountManager.login(accountId, password); + return "\""+ accountManager.login(accountId, password)+"\""; } } diff --git a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java index 1a65b7b..5dcacc7 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/BooksRest.java @@ -22,14 +22,12 @@ private final BookManager bookManager; private final AccountManager accountManager; - private final TodoManager todoManager; private final FavoriteManager favoriteManager; @Autowired // スプリングブートにいうサイン - public BooksRest(BookManager bm, AccountManager ac, TodoManager tm, FavoriteManager fm){//public クラス名()がコンストラクタ + public BooksRest(BookManager bm, AccountManager ac, FavoriteManager fm){//public クラス名()がコンストラクタ bookManager = bm; accountManager = ac; - todoManager = tm; favoriteManager = fm; } @@ -67,7 +65,9 @@ public Book getBook(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @QueryParam("token") String token){ accountCheck(account_id); - tokenCheck(account_id, token); + if (!bookManager.getPublicity(account_id, book_id)){ + tokenCheck(account_id, token); + } return bookManager.getBook(account_id, book_id); } /// 本の削除 @@ -79,9 +79,8 @@ accountCheck(account_id); tokenCheck(account_id, token); - todoManager.deleteAllTodosByBookId(account_id, book_id);//削除時、Todoも消す - favoriteManager.removeFavoriteByBookID(account_id, book_id);//削除時、Favoriteも消す bookManager.deleteBook(account_id, book_id); + favoriteManager.removeFavoriteByBookID(account_id, book_id); return "success"; } @@ -148,6 +147,26 @@ return "success"; } + @Path("/{account_id}/books/{book_id}/favoriteCount") + @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"; + } + + @Path("/{account_id}/books/{book_id}/favoriteCount") + @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"; + } + ///--------------------------------------------------------------------- ///private ///--------------------------------------------------------------------- diff --git a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java index 5a5a176..9a96b4e 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/FavoritedRest.java @@ -5,6 +5,7 @@ import javax.ws.rs.core.Response; 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; @@ -15,13 +16,16 @@ @Component public class FavoritedRest { + private final BookManager bookManager; private FavoriteManager favoriteManager; private final AccountManager accountManager; + @Autowired - public FavoritedRest(FavoriteManager favoriteManager, AccountManager accountManager){ + public FavoritedRest(FavoriteManager favoriteManager, AccountManager accountManager, BookManager bookManager){ this.favoriteManager = favoriteManager; this.accountManager = accountManager; + this.bookManager = bookManager; } @Path("/{account_id}/books/{book_id}/favorited") @GET @@ -37,11 +41,42 @@ @PUT @Produces(MediaType.APPLICATION_FORM_URLENCODED) public void putFavorited(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("other_account_id") String other_account_id, @FormParam("token") String token){ - if(accountManager.checkToken(other_account_id,token)) { - favoriteManager.putFavorited(account_id, book_id, other_account_id); - favoriteManager.putFavorites(other_account_id, account_id, book_id);//変更点(要検討) + + //accountID存在確認 + if(accountManager.getAccount(account_id) != null) { + //other_account_id存在確認 + if (accountManager.getAccount(other_account_id) != null) { + //book_id存在確認 + if (bookManager.getBook(account_id, book_id) != null) { + //本の公開状況確認 + if(bookManager.getPublicity(account_id, book_id)){ + //token認証 + if (accountManager.checkToken(other_account_id, token)) { + favoriteManager.putFavorited(account_id, book_id, other_account_id); + favoriteManager.putFavorites(other_account_id, account_id, book_id);//変更点(要検討) + } else { + //token認証失敗時のエラー表示 + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + }else{ + //本が公開されていないときのエラー表示 + var response = Response.status(Response.Status.NOT_FOUND).entity("本が公開されていません"); + throw new WebApplicationException(response.build()); + } + } else { + //本が存在しない時に対するエラー表示 + var response = Response.status(Response.Status.NOT_FOUND).entity("本が存在しません"); + throw new WebApplicationException(response.build()); + } + } else { + //other_account_idが存在しない時に対するエラー表示 + var response = Response.status(Response.Status.NOT_FOUND).entity("accountが存在しません"); + throw new WebApplicationException(response.build()); + } }else{ - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + //account_idが存在しない時に対するエラー表示 + var response = Response.status(Response.Status.NOT_FOUND).entity("accountが存在しません"); throw new WebApplicationException(response.build()); } } @@ -50,11 +85,41 @@ @DELETE @Produces(MediaType.APPLICATION_FORM_URLENCODED) public void removeFavorited(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("other_account_id") String other_account_id, @QueryParam("token") String token){ - if(accountManager.checkToken(other_account_id,token)) { - favoriteManager.removeFavorited(account_id, book_id, other_account_id); - favoriteManager.removeFavorites(other_account_id, account_id, book_id);//変更点(要検討) + //account存在確認 + if(accountManager.getAccount(account_id) != null) { + //other_account_id存在確認 + if (accountManager.getAccount(other_account_id) != null) { + //book_id存在確認 + if (bookManager.getBook(account_id, book_id) != null) { + //本の公開状況確認 + if(bookManager.getPublicity(account_id, book_id)) { + //token承認 + if (accountManager.checkToken(other_account_id, token)) { + favoriteManager.removeFavorited(account_id, book_id, other_account_id); + favoriteManager.removeFavorites(other_account_id, account_id, book_id);//変更点(要検討) + } else { + //tokenが承認しないときに対するエラー表示 + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + }else{ + //本が公開していないときに対するエラー + var response = Response.status(Response.Status.NOT_FOUND).entity("本が公開されていません"); + throw new WebApplicationException(response.build()); + } + } else { + //本が存在しない時に対するエラー表示 + var response = Response.status(Response.Status.NOT_FOUND).entity("本が存在しません"); + throw new WebApplicationException(response.build()); + } + } else { + //other_account_idが存在しない時に対するエラー表示 + var response = Response.status(Response.Status.NOT_FOUND).entity("accountが存在しません"); + throw new WebApplicationException(response.build()); + } }else{ - var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + //accountが存在しない時に対するエラー表示 + var response = Response.status(Response.Status.NOT_FOUND).entity("accountが存在しません"); throw new WebApplicationException(response.build()); } } diff --git a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java index b94a8bc..b435fc2 100644 --- a/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java +++ b/src/main/java/org/ntlab/citrusserver/resources/TodoRest.java @@ -6,8 +6,10 @@ import org.apache.coyote.http11.upgrade.UpgradeServletOutputStream; import org.ntlab.citrusserver.entities.Todo; import org.ntlab.citrusserver.repositories.AccountManager; +import org.ntlab.citrusserver.repositories.BookManager; import org.ntlab.citrusserver.repositories.TodoManager; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.cache.annotation.CachePut; import org.springframework.stereotype.Component; import java.util.HashMap; @@ -18,11 +20,13 @@ private final TodoManager todoManager; private final AccountManager accountManager; + private final BookManager bookManager; @Autowired - public TodoRest(TodoManager todoManager, AccountManager accountManager) { + public TodoRest(TodoManager todoManager, AccountManager accountManager, BookManager bookManager) { this.todoManager = todoManager; this.accountManager = accountManager; + this.bookManager = bookManager; } //test用 @@ -39,7 +43,7 @@ @GET @Produces(MediaType.APPLICATION_JSON) public HashMap>>> getAllTodos(@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 todoManager.getAllTodos(account_id, book_id); } return null; @@ -50,7 +54,7 @@ @GET @Produces(MediaType.APPLICATION_JSON) public HashMap> getTodosByMonth(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @QueryParam("token") String token) { - if (accountManager.checkToken(account_id, token)) { + if (accountManager.checkToken(account_id, token) || bookManager.getPublicity(account_id, book_id)) { return todoManager.getTodosByMonth(account_id, book_id, year, month); } return null; @@ -61,7 +65,7 @@ @GET @Produces(MediaType.APPLICATION_JSON) public HashMap getTodosByDay(@PathParam("account_id") String account_id, @PathParam("book_id") Integer book_id, @PathParam("year") Integer year, @PathParam("month") Integer month, @PathParam("day") Integer day, @QueryParam("token") String token) { - if (accountManager.checkToken(account_id, token)) { + if (accountManager.checkToken(account_id, token) || bookManager.getPublicity(account_id, book_id)) { return todoManager.getTodosByDay(account_id, book_id, year, month, day); } return null; @@ -72,7 +76,7 @@ @GET @Produces(MediaType.APPLICATION_JSON) public Todo getTodoById(@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, @QueryParam("token") String token) { - if (accountManager.checkToken(account_id, token)) { + if (accountManager.checkToken(account_id, token) || bookManager.getPublicity(account_id, book_id)) { Todo todo = todoManager.getTodoById(account_id, book_id, year, month, day, todo_id); return todo; } @@ -110,6 +114,24 @@ } } + @PUT + @Path("/{account_id}/books/{book_id}/todos/{year}/{month}/{day}/{todo_id}") + @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, + @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) { + var response = Response.status(Response.Status.BAD_REQUEST).entity("変更失敗"); + throw new WebApplicationException(response.build()); + } + }else{ + var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗"); + throw new WebApplicationException(response.build()); + } + } + /* //本のtodoを年月日とtodo_idを指定してそのtodoを削除する @DELETE