iT邦幫忙

2021 iThome 鐵人賽

DAY 16
0

挑戰目標: MockNative Camp


昨天我們建立了CommonResponse.java來做為我們的通一response格式,今天我們修改了UsersService.java來測試一下是否跟我們想的一樣。

package com.mock.nativecamp.service;

import com.mock.nativecamp.model.Users;
import com.mock.nativecamp.payload.CommonResponse;
import com.mock.nativecamp.payload.sub.ResData;
import com.mock.nativecamp.payload.user.PatchReq;
import com.mock.nativecamp.payload.user.SignupReq;
import com.mock.nativecamp.repository.UsersRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Slf4j
@Service
public class UsersService {

    @Autowired
    private UsersRepository usersRepository;

    public CommonResponse signupUser(SignupReq signupReq) {
        Users user = new Users().signUser(signupReq);
        usersRepository.save(user);
        CommonResponse commonResponse = new CommonResponse().successRes();
        ResData resData = new ResData();
        resData.setData(user);
        commonResponse.setData(resData);
        return commonResponse;
    }

    public CommonResponse getAllUsers() {
        List<Users> users = usersRepository.findAll();
        CommonResponse commonResponse = new CommonResponse().successRes();
        ResData resData = new ResData();
        resData.setData(users);
        commonResponse.setData(resData);
        return commonResponse;
    }

    public CommonResponse getUser(String id) {
        Users user = usersRepository.findByUserId(id);
        CommonResponse commonResponse = new CommonResponse().successRes();
        if (user == null) {
            commonResponse = commonResponse.errorRes("user id not found", "404");
        }
        ResData resData = new ResData();
        resData.setData(user);
        commonResponse.setData(resData);
        return commonResponse;
    }

    public CommonResponse updateUser(String userId, PatchReq patchReq) {
        Users user = usersRepository.findByUserId(userId);
        CommonResponse commonResponse = new CommonResponse().successRes();
        if (user == null) {
            commonResponse = new CommonResponse().errorRes("user id not found", "404");
        } else {
            user.updateUser(user, patchReq);
            usersRepository.save(user);
        }
        ResData resData = new ResData();
        resData.setData(user);
        commonResponse.setData(resData);
        return commonResponse;
    }
}

新增User
https://ithelp.ithome.com.tw/upload/images/20211001/20140358irrZP024ct.png
查詢所有User
https://ithelp.ithome.com.tw/upload/images/20211001/201403580FB7jPceYy.png
查詢單一User (沒找到)
https://ithelp.ithome.com.tw/upload/images/20211001/20140358WZkfTg7bQ8.png
查詢單一User
https://ithelp.ithome.com.tw/upload/images/20211001/20140358T5S0yKWJD3.png
更新User (沒找到)
https://ithelp.ithome.com.tw/upload/images/20211001/20140358lwS8p0vS8k.png
更新User
https://ithelp.ithome.com.tw/upload/images/20211001/20140358uoBxb53dkl.png

透過統一的response,前端串接時就可以很清楚的知道該從哪裡取得需要的資料,在做error handling時也會受益於統一response。
明天就來為專案加上全局性的error handling


上一篇
[Day 15]吶吶,還有一半別想跑(後端篇)
下一篇
[Day 17]獨自一人的全端攻略(後端篇)
系列文
關於我快30歲的後端工程師,想轉職成全端工程師,在前端世界中尋求機會的那件事(後端篇)18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言