์• ์ •์ฝ”๋”ฉ ๐Ÿ’ป

Project/DARLING 2022.02.16 ๋Œ“๊ธ€ 0๊ฐœ Joana

8. Service

@RequiredArgsConstructor
@Service
public class DiaryRegisterService {

  private final CoupleRepository coupleRepository;
  private final DiaryRepository diaryRepository;
  private final UserRepository userRepository;

  public DiaryRegisterResponse addDiary(DiaryRegisterRequest diaryRegisterRequest) {

    Couple couple =
        coupleRepository
            .findByCoupleToken(diaryRegisterRequest.getCoupleToken())
            .orElseThrow(() -> new IllegalArgumentException("Invalid CoupleToken"));
    Diary diary =
        diaryRepository.save(
            Diary.builder().couple(couple).date(diaryRegisterRequest.getDate()).build());

    return DiaryRegisterResponse.builder().diaryId(diary.getId()).build();
  	}
  }

 

- ResponseEnitity ๋ฅผ ์ ์šฉํ•˜๊ณ  ์‹ถ์—ˆ์ง€๋งŒ ํ”„๋ก ํŠธ ์ž‘์—…์ด ๋“ค์–ด๊ฐ€๊ธฐ ๋•Œ๋ฌธ์— Response ๋Š” ๋˜‘๊ฐ™์ด ๋งŒ๋“ค์—ˆ๋‹ค.( ๋Œ€๋ถ€๋ถ„ ๊ทธ๋Ÿด๊ฒƒ,, )

 

@RequiredArgsConstructor

์ด ์–ด๋…ธํ…Œ์ด์…˜์€ ์ดˆ๊ธฐํ™” ๋˜์ง€์•Š์€ final ํ•„๋“œ, @NonNull ์ด ๋ถ™์€ ํ•„๋“œ์— ๋Œ€ํ•ด ์ƒ์„ฑ์ž๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑํ•ด์ค๋‹ˆ๋‹ค.

์œ„์™€ ๊ฐ™์ด Repogitory ์— final ์„ ๋ถ™์—ฌ์ค€ ์ด์œ ์ž…๋‹ˆ๋‹ค. 

 

- ์ปดํŒŒ์ผ ์‹œ์ ์— ๋ˆ„๋ฝ๋œ ์˜์กด์„ฑ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

- Spring์—์„œ ์ƒ์„ฑ์ž๊ฐ€ 1๊ฐœ์ธ ๊ฒฝ์šฐ @Autowired๋ฅผ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋„๋ก ๋„์™€์ค€๋‹ค.

- ํ•ด๋‹น ์ƒ์„ฑ์ž๋Š” @RequiredArgsConstructor ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.

 

 

๋ฐ˜์‘ํ˜•

'Project > DARLING' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

7. CI/CD ๊ณ„ํš  (0) 2022.02.09
6. ๋ฌด์—‡์ด ๋ฌธ์ œ์ผ๊นŒ ...  (0) 2022.01.25
5. Spring REST Docs ์‚ฌ์šฉ  (0) 2022.01.24
4. ์—ฐ๊ด€๊ด€๊ณ„ - OneToMany  (0) 2022.01.23
3. ์—ฐ๊ด€๊ด€๊ณ„ - ManyToOne  (0) 2022.01.23