Newer
Older
CitrusServer / src / main / java / org / ntlab / citrusserver / resources / AccountsRest.java
amika uemura on 23 May 9 KB Merge pull request #158 from nitta-lab-2024/Favorited
package org.ntlab.citrusserver.resources;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import org.ntlab.citrusserver.entities.Account;
import org.ntlab.citrusserver.repositories.AccountManager;
import org.ntlab.citrusserver.repositories.BookManager;
import org.ntlab.citrusserver.repositories.ScheduleManager;
import org.ntlab.citrusserver.repositories.TodoManager;
import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;

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

@Path("/accounts")
@Component //accountRestのインスタンスを一個作る

public class AccountsRest {
    private final AccountManager accountManager; //finalは書き換えられない
    private final BookManager bookManager;
    private final TodoManager todoManager;
    private final ScheduleManager scheduleManager;

    @Autowired//springbootの決まり
    public AccountsRest(AccountManager am, BookManager bm, TodoManager tm, ScheduleManager sm) {
        accountManager = am;
        bookManager = bm;
        todoManager = tm;
        scheduleManager = sm;
    }

    // アカウントの一覧をリストとして返す(GET)
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Set<String> getAccount(){
        return accountManager.getAccountsID();
    }



    // account_idとpasswordを設定し新しいアカウントを作成する(POST)
    @POST
    @Consumes(MediaType.APPLICATION_FORM_URLENCODED)//bodyに入力する値がある時
    public String signup(@FormParam("account_id") String accountId, @FormParam("password") String password) {
        String token;
        if (password == null) {
            var response = Response.status(Response.Status.BAD_REQUEST).entity("passwordを入力してください");
            throw new WebApplicationException(response.build());
        }
        token = accountManager.createAccount(accountId, password);
        return token;
    }

    // 指定されたアカウントの情報を返す(GET)
    @Path("/{account_id}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Account getAccountInfo(@PathParam("account_id") String accountId) {
        //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.getAccount(accountId);
    }

    // アカウント情報を全削除する(DELETE)
    @Path("/{account_id}")
    @DELETE
    public void deleteAccount(@PathParam("account_id") String accountId,
                               @QueryParam("token") String token,
                               @QueryParam("password")String password) {
        if(accountManager.checkToken(accountId, token)) {
            accountManager.deleteAccount(accountId, token, password);
            bookManager.deleteAllBooks(accountId);
            todoManager.deleteAllTodosByAccountId(accountId);
            scheduleManager.deleteSchedules(accountId);
            return;
        }
        //404
        if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
            var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
            throw new WebApplicationException(response.build());
        }
        //403
        var response = Response.status(Response.Status.FORBIDDEN).entity("アカウント削除失敗");//forbiddenは403
        throw new WebApplicationException(response.build());

    }


    //指定されたIDのパスワードを変更する (PUT)
    @Path("/{account_id}/password")
    @PUT
    public void changePassword(@PathParam("account_id") String accountId,
                                  @FormParam("new_password")String newPassword,
                                  @FormParam("old_password")String oldPassword,
                                  @FormParam("token") String token){
        if(accountManager.checkToken(accountId, token)) {
            accountManager.changePassword(accountId, newPassword, oldPassword, token);
            return;
        }

        //404
        if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
            var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
            throw new WebApplicationException(response.build());
        }
        //403
        var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403
        throw new WebApplicationException(response.build());
    }

    // 指定されたIDの自己紹介を返す(GET)
    @Path("/{account_id}/introduction")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public String getIntroduction(@PathParam("account_id") String accountId){
        String ac = accountManager.AccountIntro(accountId);
        return ac;
    }

    // 指定されたIDの自己紹介を変更する (PUT)
    @Path("/{account_id}/introduction")
    @PUT
    public void changeIntroduction(@PathParam("account_id") String accountId,
                                    @FormParam("introduction")String introduction,
                                    @FormParam("token") String token){
        if(accountManager.checkToken(accountId, token)) {
            accountManager.changeIntroduction(accountId, introduction, token);
            return;
        }
        //404
        if (!accountManager.getAccountsID().contains(accountId)){ //account_idが存在しない時
            var response = Response.status(Response.Status.NOT_FOUND).entity("IDが存在しません");//404
            throw new WebApplicationException(response.build());
        }
        //403
        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


    //指定されたIDのお気に入りの本のリストを返す
    @Path("/accounts/{account_id}/favorites")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<HashMap<String, String>> favoriteBook(@PathParam("account_id") String accountId,
                                                           @QueryParam("token")String token){
        if(accountManager.checkToken(accountId, token)) {
            return accountManager.Favorites(accountId, 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());
        }
        return null;
    }

    //指定されたIDのお気に入りの本のリストを返す(指定した人物)
    @Path("/accounts/{account_id}/favorites/{other_account_id}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public ArrayList<Integer> FavoriteBook(@PathParam("account_id") String accountId,
                                           @PathParam("other_account_id") String otherAccountId,
                                           @QueryParam("token")String token){
        if(accountManager.checkToken(accountId, token)) {
            return accountManager.FavoritesBookId(accountId, otherAccountId, 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());
        }
        //403
        var response = Response.status(Response.Status.FORBIDDEN).entity("認証失敗");//forbiddenは403
        throw new WebApplicationException(response.build());
    }

//////////
// お気に入りの本の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



    // アカウントidとパスワードでログインし、tokenを返す (POST)
    @Path("/{account_id}/login")
    @POST
    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);
    }

}