Newer
Older
CitrusServer / src / main / java / org / ntlab / citrusserver / resources / ScheduleRest.java
package org.ntlab.citrusserver.resources;

import jakarta.ws.rs.core.Response;
import org.ntlab.citrusserver.entities.Schedule;
import org.ntlab.citrusserver.repositories.AccountManager;
import org.ntlab.citrusserver.repositories.ScheduleManager;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;

@Path("/accounts")
@Component

public class ScheduleRest {
    private final ScheduleManager scheduleManager;
    private final AccountManager accountManager;

    @Autowired
    public ScheduleRest(ScheduleManager sm, AccountManager ac){
        scheduleManager = sm;
        accountManager = ac;
    }

    @Path("/{account_id}/schedule")
    @GET//登録されたスケジュールのidを返す
    @Produces(MediaType.APPLICATION_JSON)
    public HashMap<Integer,HashMap<Integer,HashMap<Integer,HashMap<Integer,Schedule>>>> getScheduleAll(@PathParam("account_id") String accountId,
                                                                                                       @QueryParam("token") String token){
        if(accountManager.checkToken(accountId,token)) {
            return scheduleManager.getScheduleAll(accountId);
        }
        var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
        throw new WebApplicationException(response.build());
    }

    @Path("/{account_id}/schedule/{year}")
    @GET//登録されたスケジュールのidを年単位で返す
    @Produces(MediaType.APPLICATION_JSON)
    public HashMap<Integer,HashMap<Integer,HashMap<Integer,Schedule>>> getScheduleYear(@PathParam("account_id") String accountId,
                                                                                       @PathParam("year") Integer year,@QueryParam("token") String token){
        if(accountManager.checkToken(accountId,token)) {
            return scheduleManager.getScheduleYear(accountId,year);
        }
        var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
        throw new WebApplicationException(response.build());
    }

    @Path("/{account_id}/schedule/{year}/{month}")
    @GET//登録されたスケジュールのidを月単位で返す
    @Produces(MediaType.APPLICATION_JSON)
    public HashMap<Integer,HashMap<Integer,Schedule>> 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);
        }
        var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
        throw new WebApplicationException(response.build());
    }

    @Path("/{account_id}/schedule/{year}/{month}/{day}")
    @GET//登録されたスケジュールのidを日ごとで返す
    @Produces(MediaType.APPLICATION_JSON)
    public HashMap<Integer,Schedule> 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);
        }
        var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
        throw new WebApplicationException(response.build());
    }

    @Path("/{account_id}/schedule/{year}/{month}/{day}")
    @POST//スケジュールの新規作成
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    @Produces(MediaType.APPLICATION_JSON)
    public Schedule addSchedule(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
                                @PathParam("month") Integer month, @PathParam("day") Integer day,@FormParam("title") String title,
                                @FormParam("start_time") String startTime,@FormParam("end_time") String endTime,@FormParam("book_id") Integer bookId,
                                @FormParam("token") String token) {
        if(accountManager.checkToken(accountId,token)) {
            Schedule schedule=scheduleManager.addSchedule(accountId, year, month, day,title,startTime,endTime,bookId);
            if(schedule==null){
                var response = Response.status(Response.Status.NOT_FOUND).entity("その本は存在しません");
                throw new WebApplicationException(response.build());
            }
            return schedule;

        }else {
            var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
            throw new WebApplicationException(response.build());
        }
    }


    @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}")
    @GET//スケジュールの情報を返す
    @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) {
        if(accountManager.checkToken(accountId,token)) {
            Schedule schedule = scheduleManager.getScheduleId(accountId, year, month, day, scheduleId);
            return schedule;
        }else {
            var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
            throw new WebApplicationException(response.build());
        }
    }

    @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}")
    @DELETE//特定のスケジュールを削除する
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String deleteSchedule(@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) {
        if(accountManager.checkToken(accountId,token)) {
            scheduleManager.deleteSchedule(accountId, year, month, day, scheduleId);
            return "success";
        }else {
            var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
            throw new WebApplicationException(response.build());
        }
    }

    @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}/start_time")
    @PUT//スケジュールの開始時刻の新規作成・変更
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String setScheduleStartTime(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
                                    @PathParam("month") Integer month, @PathParam("day") Integer day,
                                    @PathParam("schedule_id") Integer scheduleId, @FormParam("token") String token, @FormParam("start_time") String startTime) {
        if(accountManager.checkToken(accountId,token)) {
            scheduleManager.setScheduleStartTime(accountId, year, month, day, scheduleId,startTime);
            return "success";
        }else {
            var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
            throw new WebApplicationException(response.build());
        }
    }

    @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}/end_time")
    @PUT//スケジュールの終了時刻の新規作成・変更
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String setScheduleEndTime(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
                                         @PathParam("month") Integer month, @PathParam("day") Integer day,
                                         @PathParam("schedule_id") Integer scheduleId, @FormParam("token") String token, @FormParam("end_time") String endTime) {
        if(accountManager.checkToken(accountId,token)) {
            scheduleManager.setScheduleEndTime(accountId, year, month, day, scheduleId, endTime);
            return "success";
        }else {
            var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
            throw new WebApplicationException(response.build());
        }
    }

    @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}/title")
    @PUT//スケジュールのタイトルの変更
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String setScheduleTitle(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
                                    @PathParam("month") Integer month, @PathParam("day") Integer day,
                                    @PathParam("schedule_id") Integer scheduleId, @FormParam("token") String token, @FormParam("title") String title) {
        if(accountManager.checkToken(accountId,token)) {
            scheduleManager.changeScheduleTitle(accountId, year, month, day, scheduleId, title);
            return "success";
        }else {
            var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
            throw new WebApplicationException(response.build());
        }
    }

    @Path("/{account_id}/schedule/{year}/{month}/{day}/{schedule_id}/book_id")
    @PUT//スケジュールの所属している本の新規設定・変更
    @Produces(MediaType.TEXT_PLAIN)
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)
    public String setSchedulesBook(@PathParam("account_id") String accountId, @PathParam("year") Integer year,
                                         @PathParam("month") Integer month, @PathParam("day") Integer day,
                                         @PathParam("schedule_id") Integer scheduleId, @FormParam("token") String token, @FormParam("book_id") Integer bookId) {
        if(accountManager.checkToken(accountId,token)) {
            int x=scheduleManager.setSchedulesBookId(accountId, year, month, day, scheduleId, bookId);
            if(x==-1){
                return "その本は存在していません";
            }else{
                return "success";
            }
        }else {
            var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");
            throw new WebApplicationException(response.build());
        }
    }
}