Newer
Older
CitrusServer / src / main / java / org / ntlab / citrusserver / resources / ScheduleRest.java
  1. package org.ntlab.citrusserver.resources;
  2.  
  3. import jakarta.ws.rs.core.Response;
  4. import org.ntlab.citrusserver.entities.Schedule;
  5. import org.ntlab.citrusserver.repositories.AccountManager;
  6. import org.ntlab.citrusserver.repositories.ScheduleManager;
  7. import jakarta.ws.rs.*;
  8. import jakarta.ws.rs.core.MediaType;
  9. import org.springframework.beans.factory.annotation.Autowired;
  10. import org.springframework.stereotype.Component;
  11.  
  12. import java.util.ArrayList;
  13. import java.util.HashMap;
  14.  
  15. @Path("/accounts")
  16. @Component
  17.  
  18. public class ScheduleRest {
  19. private final ScheduleManager scheduleManager;
  20. private final AccountManager accountManager;
  21.  
  22. @Autowired
  23. public ScheduleRest(ScheduleManager sm, AccountManager ac){
  24. scheduleManager = sm;
  25. accountManager = ac;
  26. }
  27.  
  28. @Path("/{account_id}/schedule")
  29. @GET//登録されたスケジュールのidを返す
  30. @Produces(MediaType.APPLICATION_JSON)
  31. public HashMap<Integer,HashMap<Integer,HashMap<Integer,HashMap<Integer,Schedule>>>> getScheduleAll(@PathParam("account_id") String accountId,
  32. @QueryParam("token") String token){
  33. if(accountManager.checkToken(accountId,token)) {
  34. return scheduleManager.getScheduleAll(accountId);
  35. }
  36. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  37. throw new WebApplicationException(response.build());
  38. }
  39.  
  40. @Path("/{account_id}/schedule/{year}")
  41. @GET//登録されたスケジュールのidを年単位で返す
  42. @Produces(MediaType.APPLICATION_JSON)
  43. public HashMap<Integer,HashMap<Integer,HashMap<Integer,Schedule>>> getScheduleYear(@PathParam("account_id") String accountId,
  44. @PathParam("year") Integer year,@QueryParam("token") String token){
  45. if(accountManager.checkToken(accountId,token)) {
  46. return scheduleManager.getScheduleYear(accountId,year);
  47. }
  48. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  49. throw new WebApplicationException(response.build());
  50. }
  51.  
  52. @Path("/{account_id}/schedule/{year}/{month}")
  53. @GET//登録されたスケジュールのidを月単位で返す
  54. @Produces(MediaType.APPLICATION_JSON)
  55. public HashMap<Integer,HashMap<Integer,Schedule>> getScheduleMonth(@PathParam("account_id") String accountId,
  56. @PathParam("year") Integer year,@PathParam("month") Integer month,
  57. @QueryParam("token") String token){
  58. if(accountManager.checkToken(accountId,token)) {
  59. return scheduleManager.getScheduleMonth(accountId,year,month);
  60. }
  61. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  62. throw new WebApplicationException(response.build());
  63. }
  64.  
  65. @Path("/{account_id}/schedule/{year}/{month}/{day}")
  66. @GET//登録されたスケジュールのidを日ごとで返す
  67. @Produces(MediaType.APPLICATION_JSON)
  68. public HashMap<Integer,Schedule> getScheduleDay(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
  69. @PathParam("month") Integer month, @PathParam("day") Integer day,
  70. @QueryParam("token") String token){
  71. if(accountManager.checkToken(accountId,token)) {
  72. return scheduleManager.getScheduleDay(accountId,year,month,day);
  73. }
  74. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  75. throw new WebApplicationException(response.build());
  76. }
  77.  
  78. @Path("/{account_id}/schedule/{year}/{month}/{day}")
  79. @POST//スケジュールの新規作成
  80. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  81. @Produces(MediaType.APPLICATION_JSON)
  82. public Schedule addSchedule(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
  83. @PathParam("month") Integer month, @PathParam("day") Integer day,@FormParam("title") String title,
  84. @FormParam("start_time") String startTime,@FormParam("end_time") String endTime,@FormParam("book_id") Integer bookId,
  85. @FormParam("token") String token) {
  86. if(accountManager.checkToken(accountId,token)) {
  87. Schedule schedule=scheduleManager.addSchedule(accountId, year, month, day,title,startTime,endTime,bookId);
  88. if(schedule==null){
  89. var response = Response.status(Response.Status.NOT_FOUND).entity("その本は存在しません");
  90. throw new WebApplicationException(response.build());
  91. }
  92. return schedule;
  93.  
  94. }else {
  95. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  96. throw new WebApplicationException(response.build());
  97. }
  98. }
  99.  
  100.  
  101. @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}")
  102. @GET//スケジュールの情報を返す
  103. @Produces(MediaType.APPLICATION_JSON)
  104. public Schedule getScheduleInfo(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
  105. @PathParam("month") Integer month, @PathParam("day") Integer day,
  106. @PathParam("schedule_id") Integer scheduleId, @QueryParam("token") String token) {
  107. if(accountManager.checkToken(accountId,token)) {
  108. Schedule schedule = scheduleManager.getScheduleId(accountId, year, month, day, scheduleId);
  109. return schedule;
  110. }else {
  111. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  112. throw new WebApplicationException(response.build());
  113. }
  114. }
  115.  
  116. @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}")
  117. @DELETE//特定のスケジュールを削除する
  118. @Produces(MediaType.TEXT_PLAIN)
  119. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  120. public String deleteSchedule(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
  121. @PathParam("month") Integer month, @PathParam("day") Integer day,
  122. @PathParam("schedule_id") Integer scheduleId, @QueryParam("token") String token) {
  123. if(accountManager.checkToken(accountId,token)) {
  124. scheduleManager.deleteSchedule(accountId, year, month, day, scheduleId);
  125. return "success";
  126. }else {
  127. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  128. throw new WebApplicationException(response.build());
  129. }
  130. }
  131.  
  132. @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}/start_time")
  133. @PUT//スケジュールの開始時刻の新規作成・変更
  134. @Produces(MediaType.TEXT_PLAIN)
  135. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  136. public String setScheduleStartTime(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
  137. @PathParam("month") Integer month, @PathParam("day") Integer day,
  138. @PathParam("schedule_id") Integer scheduleId, @FormParam("token") String token, @FormParam("start_time") String startTime) {
  139. if(accountManager.checkToken(accountId,token)) {
  140. scheduleManager.setScheduleStartTime(accountId, year, month, day, scheduleId,startTime);
  141. return "success";
  142. }else {
  143. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  144. throw new WebApplicationException(response.build());
  145. }
  146. }
  147.  
  148. @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}/end_time")
  149. @PUT//スケジュールの終了時刻の新規作成・変更
  150. @Produces(MediaType.TEXT_PLAIN)
  151. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  152. public String setScheduleEndTime(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
  153. @PathParam("month") Integer month, @PathParam("day") Integer day,
  154. @PathParam("schedule_id") Integer scheduleId, @FormParam("token") String token, @FormParam("end_time") String endTime) {
  155. if(accountManager.checkToken(accountId,token)) {
  156. scheduleManager.setScheduleEndTime(accountId, year, month, day, scheduleId, endTime);
  157. return "success";
  158. }else {
  159. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  160. throw new WebApplicationException(response.build());
  161. }
  162. }
  163.  
  164. @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}/title")
  165. @PUT//スケジュールのタイトルの変更
  166. @Produces(MediaType.TEXT_PLAIN)
  167. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  168. public String setScheduleTitle(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
  169. @PathParam("month") Integer month, @PathParam("day") Integer day,
  170. @PathParam("schedule_id") Integer scheduleId, @FormParam("token") String token, @FormParam("title") String title) {
  171. if(accountManager.checkToken(accountId,token)) {
  172. scheduleManager.changeScheduleTitle(accountId, year, month, day, scheduleId, title);
  173. return "success";
  174. }else {
  175. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  176. throw new WebApplicationException(response.build());
  177. }
  178. }
  179.  
  180. @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}/book_id")
  181. @PUT//スケジュールの所属している本の新規設定・変更
  182. @Produces(MediaType.TEXT_PLAIN)
  183. @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
  184. public String setSchedulesBook(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
  185. @PathParam("month") Integer month, @PathParam("day") Integer day,
  186. @PathParam("schedule_id") Integer scheduleId, @FormParam("token") String token, @FormParam("book_id") Integer bookId) {
  187. if(accountManager.checkToken(accountId,token)) {
  188. int x=scheduleManager.setSchedulesBookId(accountId, year, month, day, scheduleId, bookId);
  189. if(x==-1){
  190. var response = Response.status(Response.Status.NOT_FOUND).entity("その本は存在しません");
  191. throw new WebApplicationException(response.build());
  192. }else{
  193. return "success";
  194. }
  195. }else {
  196. var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
  197. throw new WebApplicationException(response.build());
  198. }
  199. }
  200. }