From 62a4ffd438da13dba97b182e086408c02a0f6da6 Mon Sep 17 00:00:00 2001 From: Yangdaehan Date: Thu, 2 May 2024 02:16:54 +0900 Subject: [PATCH 1/2] afdSF --- week03/src/main/java/.DS_Store | Bin 0 -> 6148 bytes week03/src/main/java/org/.DS_Store | Bin 0 -> 6148 bytes week03/src/main/java/org/sopt/.DS_Store | Bin 0 -> 6148 bytes .../java/org/sopt/spring/Application.java | 13 +++++ .../spring/common/GlobalExceptionHandler.java | 27 ++++++++++ .../sopt/spring/common/dto/ErrorMessage.java | 16 ++++++ .../sopt/spring/common/dto/ErrorResponse.java | 14 ++++++ .../spring/common/dto/SuccessMessage.java | 18 +++++++ .../common/dto/SuccessStatusResponse.java | 11 ++++ .../sopt/spring/config/JpaAuditingConfig.java | 9 ++++ .../spring/controller/BlogController.java | 46 +++++++++++++++++ .../spring/controller/MemberController.java | 47 ++++++++++++++++++ .../spring/controller/PostController.java | 28 +++++++++++ .../sopt/spring/domain/BaseTimeEntity.java | 21 ++++++++ .../java/org/sopt/spring/domain/Blog.java | 45 +++++++++++++++++ .../java/org/sopt/spring/domain/Member.java | 43 ++++++++++++++++ .../java/org/sopt/spring/domain/Part.java | 10 ++++ .../java/org/sopt/spring/domain/Post.java | 42 ++++++++++++++++ .../spring/exception/BusinessException.java | 14 ++++++ .../spring/exception/NotFoundException.java | 9 ++++ .../spring/repository/BlogRepository.java | 7 +++ .../spring/repository/MemberRepository.java | 8 +++ .../spring/repository/PostRepository.java | 9 ++++ .../org/sopt/spring/service/BlogService.java | 38 ++++++++++++++ .../sopt/spring/service/MemberService.java | 46 +++++++++++++++++ .../org/sopt/spring/service/PostService.java | 23 +++++++++ .../spring/service/dto/BlogCreateRequest.java | 7 +++ .../service/dto/BlogTitleUpdateRequest.java | 8 +++ .../spring/service/dto/MemberCreateDto.java | 10 ++++ .../spring/service/dto/MemberFindDto.java | 14 ++++++ .../spring/service/dto/PostCreateRequest.java | 8 +++ 31 files changed, 591 insertions(+) create mode 100644 week03/src/main/java/.DS_Store create mode 100644 week03/src/main/java/org/.DS_Store create mode 100644 week03/src/main/java/org/sopt/.DS_Store create mode 100644 week03/src/main/java/org/sopt/spring/Application.java create mode 100644 week03/src/main/java/org/sopt/spring/common/GlobalExceptionHandler.java create mode 100644 week03/src/main/java/org/sopt/spring/common/dto/ErrorMessage.java create mode 100644 week03/src/main/java/org/sopt/spring/common/dto/ErrorResponse.java create mode 100644 week03/src/main/java/org/sopt/spring/common/dto/SuccessMessage.java create mode 100644 week03/src/main/java/org/sopt/spring/common/dto/SuccessStatusResponse.java create mode 100644 week03/src/main/java/org/sopt/spring/config/JpaAuditingConfig.java create mode 100644 week03/src/main/java/org/sopt/spring/controller/BlogController.java create mode 100644 week03/src/main/java/org/sopt/spring/controller/MemberController.java create mode 100644 week03/src/main/java/org/sopt/spring/controller/PostController.java create mode 100644 week03/src/main/java/org/sopt/spring/domain/BaseTimeEntity.java create mode 100644 week03/src/main/java/org/sopt/spring/domain/Blog.java create mode 100644 week03/src/main/java/org/sopt/spring/domain/Member.java create mode 100644 week03/src/main/java/org/sopt/spring/domain/Part.java create mode 100644 week03/src/main/java/org/sopt/spring/domain/Post.java create mode 100644 week03/src/main/java/org/sopt/spring/exception/BusinessException.java create mode 100644 week03/src/main/java/org/sopt/spring/exception/NotFoundException.java create mode 100644 week03/src/main/java/org/sopt/spring/repository/BlogRepository.java create mode 100644 week03/src/main/java/org/sopt/spring/repository/MemberRepository.java create mode 100644 week03/src/main/java/org/sopt/spring/repository/PostRepository.java create mode 100644 week03/src/main/java/org/sopt/spring/service/BlogService.java create mode 100644 week03/src/main/java/org/sopt/spring/service/MemberService.java create mode 100644 week03/src/main/java/org/sopt/spring/service/PostService.java create mode 100644 week03/src/main/java/org/sopt/spring/service/dto/BlogCreateRequest.java create mode 100644 week03/src/main/java/org/sopt/spring/service/dto/BlogTitleUpdateRequest.java create mode 100644 week03/src/main/java/org/sopt/spring/service/dto/MemberCreateDto.java create mode 100644 week03/src/main/java/org/sopt/spring/service/dto/MemberFindDto.java create mode 100644 week03/src/main/java/org/sopt/spring/service/dto/PostCreateRequest.java diff --git a/week03/src/main/java/.DS_Store b/week03/src/main/java/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..e6dc460bb1c6d2efab7037b03d06d0556d348790 GIT binary patch literal 6148 zcmeH~Jqp4=5QS%qgvIYIqf#4$mI7itHYo8^+Vg;}!TM!kPMk^St>SKu2 zy&WuhT}`%Nw2S8Op?PPuDF&v|E?SVlv^p5502LT1&_&+c`M-yMoBu~GOsN1B_%j7` zzB}x;c&R*FKVHx3$E@1A!9l+q;q4~?i569HJ~5`q`*vpb z-b?`-4yVJOFO_HOuh;YXIjg>IbTTeyc=-un;z#iZJ&gOs7i3MgPF8685eOL+q`h6Ww+l6f_83=FzxHXoWdYj!B=x8wZc>7q4|BNdjsxYwP53R%;7<4Y!;N+zfN4VDNGb^m2@amE(mc bMP9Kv_G{u8=yb%L4&={(=|ZCdf33h3%dHim literal 0 HcmV?d00001 diff --git a/week03/src/main/java/org/sopt/spring/Application.java b/week03/src/main/java/org/sopt/spring/Application.java new file mode 100644 index 0000000..9331693 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/Application.java @@ -0,0 +1,13 @@ +package org.sopt.spring; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/week03/src/main/java/org/sopt/spring/common/GlobalExceptionHandler.java b/week03/src/main/java/org/sopt/spring/common/GlobalExceptionHandler.java new file mode 100644 index 0000000..03cbeec --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/common/GlobalExceptionHandler.java @@ -0,0 +1,27 @@ +package org.sopt.spring.common; + + +import jakarta.persistence.EntityNotFoundException; +import org.sopt.spring.common.dto.ErrorMessage; +import org.sopt.spring.exception.NotFoundException; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.MethodArgumentNotValidException; +import org.springframework.web.bind.annotation.ExceptionHandler; +import org.springframework.web.bind.annotation.RestControllerAdvice; +import org.sopt.spring.common.dto.ErrorResponse; + +import java.util.Objects; + +@RestControllerAdvice +public class GlobalExceptionHandler { + + @ExceptionHandler(MethodArgumentNotValidException.class) + protected ResponseEntity handleMethodArgumentNotValidException(MethodArgumentNotValidException e){ + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(ErrorResponse.of(HttpStatus.BAD_REQUEST.value(), Objects.requireNonNull(e.getBindingResult().getFieldError()).getDefaultMessage())); + } + @ExceptionHandler(NotFoundException.class) + protected ResponseEntity handleNotFoundException(NotFoundException e) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorResponse.of(e.getErrorMessage())); + } +} diff --git a/week03/src/main/java/org/sopt/spring/common/dto/ErrorMessage.java b/week03/src/main/java/org/sopt/spring/common/dto/ErrorMessage.java new file mode 100644 index 0000000..f9390e1 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/common/dto/ErrorMessage.java @@ -0,0 +1,16 @@ +package org.sopt.spring.common.dto; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +@AllArgsConstructor +public enum ErrorMessage { + MEMBER_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 사용자가 존재하지 않습니다."), + + BLOG_NOT_FOUND(HttpStatus.NOT_FOUND.value(), "ID에 해당하는 블로그가 없습니다."), + ; + private final int status; + private final String message; +} diff --git a/week03/src/main/java/org/sopt/spring/common/dto/ErrorResponse.java b/week03/src/main/java/org/sopt/spring/common/dto/ErrorResponse.java new file mode 100644 index 0000000..3a645b7 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/common/dto/ErrorResponse.java @@ -0,0 +1,14 @@ +package org.sopt.spring.common.dto; + +public record ErrorResponse( + int status, + String message +) { + public static ErrorResponse of(int status, String message) { + return new ErrorResponse(status, message); + } + + public static ErrorResponse of(ErrorMessage errorMessage) { + return new ErrorResponse(errorMessage.getStatus(), errorMessage.getMessage()); + } +} diff --git a/week03/src/main/java/org/sopt/spring/common/dto/SuccessMessage.java b/week03/src/main/java/org/sopt/spring/common/dto/SuccessMessage.java new file mode 100644 index 0000000..8e87c83 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/common/dto/SuccessMessage.java @@ -0,0 +1,18 @@ +package org.sopt.spring.common.dto; + + +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.springframework.http.HttpStatus; + +@Getter +@AllArgsConstructor +public enum SuccessMessage { + + BLOG_CREATE_SUCCESS(HttpStatus.CREATED.value(),"블로그 생성이 완료되었습니다."), + POST_CREATE_SUCCESS(HttpStatus.CREATED.value(), "게시글이 생성되었습니다."); + + + private final int status; + private final String message; +} diff --git a/week03/src/main/java/org/sopt/spring/common/dto/SuccessStatusResponse.java b/week03/src/main/java/org/sopt/spring/common/dto/SuccessStatusResponse.java new file mode 100644 index 0000000..381219c --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/common/dto/SuccessStatusResponse.java @@ -0,0 +1,11 @@ +package org.sopt.spring.common.dto; + +public record SuccessStatusResponse( + int status, + String message +) { + + public static SuccessStatusResponse of(SuccessMessage successMessage) { + return new SuccessStatusResponse(successMessage.getStatus(), successMessage.getMessage()); + } +} diff --git a/week03/src/main/java/org/sopt/spring/config/JpaAuditingConfig.java b/week03/src/main/java/org/sopt/spring/config/JpaAuditingConfig.java new file mode 100644 index 0000000..1d247eb --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/config/JpaAuditingConfig.java @@ -0,0 +1,9 @@ +package org.sopt.spring.config; + +import org.springframework.context.annotation.Configuration; +import org.springframework.data.jpa.repository.config.EnableJpaAuditing; + +@EnableJpaAuditing +@Configuration +public class JpaAuditingConfig { +} diff --git a/week03/src/main/java/org/sopt/spring/controller/BlogController.java b/week03/src/main/java/org/sopt/spring/controller/BlogController.java new file mode 100644 index 0000000..3651c78 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/controller/BlogController.java @@ -0,0 +1,46 @@ +package org.sopt.spring.controller; + +import jakarta.validation.Valid; +import lombok.RequiredArgsConstructor; +import org.sopt.spring.common.dto.SuccessMessage; +import org.sopt.spring.common.dto.SuccessStatusResponse; +import org.sopt.spring.service.BlogService; +import org.sopt.spring.service.dto.BlogCreateRequest; +import org.sopt.spring.service.dto.BlogTitleUpdateRequest; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PatchMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/api/v1") +@RequiredArgsConstructor +public class BlogController { + + private final BlogService blogService; + + @PostMapping("/blog") + public ResponseEntity createBlog( + @RequestHeader(name = "memberId") Long memberId, + @RequestBody BlogCreateRequest blogCreateRequest + ) { + return ResponseEntity.status(HttpStatus.CREATED).header( + "Location", + blogService.create(memberId, blogCreateRequest)) + .body(SuccessStatusResponse.of(SuccessMessage.BLOG_CREATE_SUCCESS)); + } + + @PatchMapping("/blog/{blogId}/title") + public ResponseEntity updateBlogTitle( + @PathVariable Long blogId, + @Valid @RequestBody BlogTitleUpdateRequest blogTitleUpdateRequest + ) { + blogService.updateTitle(blogId, blogTitleUpdateRequest); + return ResponseEntity.noContent().build(); + } +} diff --git a/week03/src/main/java/org/sopt/spring/controller/MemberController.java b/week03/src/main/java/org/sopt/spring/controller/MemberController.java new file mode 100644 index 0000000..ab92021 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/controller/MemberController.java @@ -0,0 +1,47 @@ +package org.sopt.spring.controller; + + +import lombok.RequiredArgsConstructor; +import org.sopt.spring.service.MemberService; +import org.sopt.spring.service.dto.MemberCreateDto; +import org.sopt.spring.service.dto.MemberFindDto; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import java.net.URI; + +@RestController +@RequiredArgsConstructor +@RequestMapping("/api/v1/member") +public class MemberController { + + private final MemberService memberService; + + @PostMapping + public ResponseEntity createMember( + @RequestBody MemberCreateDto memberCreateDto + ) { + return ResponseEntity.created(URI.create(memberService.createMember(memberCreateDto))) + .build(); + } + + @GetMapping("/{memberId}") + public ResponseEntity findMemberById(@PathVariable Long memberId) { + return ResponseEntity.ok(memberService.findMemberById(memberId)); + } + + @DeleteMapping("/{memberId}") + public ResponseEntity deleteMemberById(@PathVariable Long memberId){ + memberService.deleteMemberById(memberId); + return ResponseEntity.noContent().build(); + } + + +} diff --git a/week03/src/main/java/org/sopt/spring/controller/PostController.java b/week03/src/main/java/org/sopt/spring/controller/PostController.java new file mode 100644 index 0000000..8ac1cf8 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/controller/PostController.java @@ -0,0 +1,28 @@ +package org.sopt.spring.controller; + +import lombok.RequiredArgsConstructor; +import org.sopt.spring.common.dto.SuccessStatusResponse; +import org.sopt.spring.service.PostService; +import org.sopt.spring.service.dto.PostCreateRequest; +import org.sopt.spring.common.dto.SuccessMessage; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/api/v1") +@RequiredArgsConstructor +public class PostController { + + private final PostService postService; + @PostMapping("/posts") + public ResponseEntity createPost( + @RequestHeader Long blogId, + @RequestBody PostCreateRequest postCreateRequest) { + return ResponseEntity.status(HttpStatus.CREATED).header( + "Location", + postService.create(blogId, postCreateRequest)) + .body(SuccessStatusResponse.of(SuccessMessage.POST_CREATE_SUCCESS)); + } +} + diff --git a/week03/src/main/java/org/sopt/spring/domain/BaseTimeEntity.java b/week03/src/main/java/org/sopt/spring/domain/BaseTimeEntity.java new file mode 100644 index 0000000..0ea75c9 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/domain/BaseTimeEntity.java @@ -0,0 +1,21 @@ +package org.sopt.spring.domain; + +import jakarta.persistence.EntityListeners; +import jakarta.persistence.MappedSuperclass; +import org.springframework.data.annotation.CreatedDate; +import org.springframework.data.annotation.LastModifiedDate; +import org.springframework.data.jpa.domain.support.AuditingEntityListener; + +import java.time.LocalDateTime; + + +@MappedSuperclass +@EntityListeners(AuditingEntityListener.class) +public abstract class BaseTimeEntity { + + @CreatedDate + private LocalDateTime createdAt; + + @LastModifiedDate + private LocalDateTime updatedAt; +} diff --git a/week03/src/main/java/org/sopt/spring/domain/Blog.java b/week03/src/main/java/org/sopt/spring/domain/Blog.java new file mode 100644 index 0000000..6b10079 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/domain/Blog.java @@ -0,0 +1,45 @@ +package org.sopt.spring.domain; + +import jakarta.persistence.Column; +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.OneToOne; +import lombok.Getter; +import lombok.NoArgsConstructor; +import org.sopt.spring.service.dto.BlogCreateRequest; +import org.sopt.spring.service.dto.BlogTitleUpdateRequest; + +@Entity +@Getter +@NoArgsConstructor +public class Blog extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + @OneToOne(fetch = FetchType.LAZY) + private Member member; + + @Column(length = 200) + private String title; + + private String description; + + private Blog(Member member, String title, String description) { + this.member = member; + this.title = title; + this.description = description; + } + + public static Blog create(Member member, BlogCreateRequest blogCreateRequest) { + return new Blog(member, blogCreateRequest.title(), blogCreateRequest.description()); + } + + public void updateTitle(BlogTitleUpdateRequest blogTitleUpdateRequest) { + this.title = blogTitleUpdateRequest.title(); + } +} diff --git a/week03/src/main/java/org/sopt/spring/domain/Member.java b/week03/src/main/java/org/sopt/spring/domain/Member.java new file mode 100644 index 0000000..e743ed7 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/domain/Member.java @@ -0,0 +1,43 @@ +package org.sopt.spring.domain; + +import jakarta.persistence.Entity; +import jakarta.persistence.EnumType; +import jakarta.persistence.Enumerated; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor +public class Member { + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + private String name; + + @Enumerated(EnumType.STRING) + private Part part; + + private int age; + + @Builder + private Member(String name, Part part, int age) { + this.name = name; + this.part = part; + this.age = age; + } + + public static Member create(String name, Part part, int age) { + return Member.builder() + .name(name) + .age(age) + .part(part) + .build(); + } +} diff --git a/week03/src/main/java/org/sopt/spring/domain/Part.java b/week03/src/main/java/org/sopt/spring/domain/Part.java new file mode 100644 index 0000000..eefaf1e --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/domain/Part.java @@ -0,0 +1,10 @@ +package org.sopt.spring.domain; + +public enum Part { + IOS, + SERVER, + ANDROID, + WEB, + PLAN, + DESIGN +} diff --git a/week03/src/main/java/org/sopt/spring/domain/Post.java b/week03/src/main/java/org/sopt/spring/domain/Post.java new file mode 100644 index 0000000..682e34b --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/domain/Post.java @@ -0,0 +1,42 @@ +package org.sopt.spring.domain; + + +import jakarta.persistence.Entity; +import jakarta.persistence.FetchType; +import jakarta.persistence.GeneratedValue; +import jakarta.persistence.GenerationType; +import jakarta.persistence.Id; +import jakarta.persistence.ManyToOne; +import lombok.Getter; +import lombok.NoArgsConstructor; + +@Entity +@Getter +@NoArgsConstructor +public class Post extends BaseTimeEntity { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private Long id; + + private String title; + + private String content; + + @ManyToOne(fetch = FetchType.LAZY) + private Blog blog; + + public Post(String title, String content, Blog blog) { + this.title = title; + this.content = content; + this.blog = blog; + } + + public static Post create(Blog blog, String title, String content) { + Post post = new Post(); + post.blog = blog; + post.title = title; + post.content = content; + return post; + } +} diff --git a/week03/src/main/java/org/sopt/spring/exception/BusinessException.java b/week03/src/main/java/org/sopt/spring/exception/BusinessException.java new file mode 100644 index 0000000..83c344e --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/exception/BusinessException.java @@ -0,0 +1,14 @@ +package org.sopt.spring.exception; + +import lombok.Getter; +import org.sopt.spring.common.dto.ErrorMessage; + +@Getter +public class BusinessException extends RuntimeException{ + private ErrorMessage errorMessage; + + public BusinessException(ErrorMessage errorMessage) { + super(errorMessage.getMessage()); + this.errorMessage = errorMessage; + } +} diff --git a/week03/src/main/java/org/sopt/spring/exception/NotFoundException.java b/week03/src/main/java/org/sopt/spring/exception/NotFoundException.java new file mode 100644 index 0000000..41ddc67 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/exception/NotFoundException.java @@ -0,0 +1,9 @@ +package org.sopt.spring.exception; + +import org.sopt.spring.common.dto.ErrorMessage; + +public class NotFoundException extends BusinessException { + public NotFoundException(ErrorMessage errorMessage) { + super(errorMessage); + } +} diff --git a/week03/src/main/java/org/sopt/spring/repository/BlogRepository.java b/week03/src/main/java/org/sopt/spring/repository/BlogRepository.java new file mode 100644 index 0000000..c039b2d --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/repository/BlogRepository.java @@ -0,0 +1,7 @@ +package org.sopt.spring.repository; + +import org.sopt.spring.domain.Blog; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface BlogRepository extends JpaRepository { +} \ No newline at end of file diff --git a/week03/src/main/java/org/sopt/spring/repository/MemberRepository.java b/week03/src/main/java/org/sopt/spring/repository/MemberRepository.java new file mode 100644 index 0000000..89331e0 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/repository/MemberRepository.java @@ -0,0 +1,8 @@ +package org.sopt.spring.repository; + +import org.sopt.spring.domain.Member; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface MemberRepository extends JpaRepository { + +} diff --git a/week03/src/main/java/org/sopt/spring/repository/PostRepository.java b/week03/src/main/java/org/sopt/spring/repository/PostRepository.java new file mode 100644 index 0000000..b97f8f8 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/repository/PostRepository.java @@ -0,0 +1,9 @@ +package org.sopt.spring.repository; + +import org.sopt.spring.domain.Post; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface PostRepository extends JpaRepository { + + +} diff --git a/week03/src/main/java/org/sopt/spring/service/BlogService.java b/week03/src/main/java/org/sopt/spring/service/BlogService.java new file mode 100644 index 0000000..f09f560 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/service/BlogService.java @@ -0,0 +1,38 @@ +package org.sopt.spring.service; + +import jakarta.transaction.Transactional; +import lombok.RequiredArgsConstructor; +import org.sopt.spring.common.dto.ErrorMessage; +import org.sopt.spring.domain.Blog; +import org.sopt.spring.domain.Member; +import org.sopt.spring.exception.NotFoundException; +import org.sopt.spring.repository.BlogRepository; +import org.sopt.spring.service.dto.BlogCreateRequest; +import org.sopt.spring.service.dto.BlogTitleUpdateRequest; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class BlogService { + private final BlogRepository blogRepository; + private final MemberService memberService; + + + public String create(Long memberId, BlogCreateRequest blogCreateRequest) { + Member member = memberService.findById(memberId); + Blog blog = blogRepository.save(Blog.create(member, blogCreateRequest)); + return blog.getId().toString(); + } + + protected Blog findById(Long blogId) { + return blogRepository.findById(blogId).orElseThrow( + () -> new NotFoundException(ErrorMessage.BLOG_NOT_FOUND) + ); + } + + @Transactional + public void updateTitle(Long blogId, BlogTitleUpdateRequest blogTitleUpdateRequest) { + Blog blog = findById(blogId); + blog.updateTitle(blogTitleUpdateRequest); + } +} diff --git a/week03/src/main/java/org/sopt/spring/service/MemberService.java b/week03/src/main/java/org/sopt/spring/service/MemberService.java new file mode 100644 index 0000000..afee2f9 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/service/MemberService.java @@ -0,0 +1,46 @@ +package org.sopt.spring.service; + +import jakarta.persistence.EntityNotFoundException; +import lombok.RequiredArgsConstructor; +import org.sopt.spring.common.dto.ErrorMessage; +import org.sopt.spring.domain.Member; +import org.sopt.spring.exception.NotFoundException; +import org.sopt.spring.repository.MemberRepository; +import org.sopt.spring.service.dto.MemberCreateDto; +import org.sopt.spring.service.dto.MemberFindDto; +import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; + +@Service +@RequiredArgsConstructor +public class MemberService { + + private final MemberRepository memberRepository; + + @Transactional + public String createMember( + MemberCreateDto memberCreateDto + ) { + Member member = Member.create(memberCreateDto.name(), memberCreateDto.part(), memberCreateDto.age()); + memberRepository.save(member); + return member.getId().toString(); + } + + public Member findById(Long memberId) { + return memberRepository.findById(memberId).orElseThrow( + () -> new NotFoundException(ErrorMessage.MEMBER_NOT_FOUND) + ); + } + public MemberFindDto findMemberById(Long memberId) { + return MemberFindDto.of(memberRepository.findById(memberId).orElseThrow( + () -> new EntityNotFoundException("ID에 해당하는 사용자가 존재하지 않습니다.") + )); + } + + @Transactional + public void deleteMemberById(Long memberId) { + Member member = memberRepository.findById(memberId) + .orElseThrow(() -> new EntityNotFoundException("ID에 해당하는 사용자가 존재하지 않습니다.")); + memberRepository.delete(member); + } +} diff --git a/week03/src/main/java/org/sopt/spring/service/PostService.java b/week03/src/main/java/org/sopt/spring/service/PostService.java new file mode 100644 index 0000000..5ca4ce9 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/service/PostService.java @@ -0,0 +1,23 @@ +package org.sopt.spring.service; + +import lombok.RequiredArgsConstructor; +import org.sopt.spring.domain.Blog; +import org.sopt.spring.domain.Post; +import org.sopt.spring.repository.PostRepository; +import org.sopt.spring.service.dto.PostCreateRequest; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class PostService { + + private final PostRepository postRepository; + private final BlogService blogService; + public String create(Long blogId, PostCreateRequest postCreateRequest) { + Blog blog = blogService.findById(blogId); + Post post = new Post(postCreateRequest.title(), postCreateRequest.content(),blog); + post = postRepository.save(post); + return post.getId().toString(); + } +} + diff --git a/week03/src/main/java/org/sopt/spring/service/dto/BlogCreateRequest.java b/week03/src/main/java/org/sopt/spring/service/dto/BlogCreateRequest.java new file mode 100644 index 0000000..76a43d6 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/service/dto/BlogCreateRequest.java @@ -0,0 +1,7 @@ +package org.sopt.spring.service.dto; + +public record BlogCreateRequest( + String title, + String description +) { +} diff --git a/week03/src/main/java/org/sopt/spring/service/dto/BlogTitleUpdateRequest.java b/week03/src/main/java/org/sopt/spring/service/dto/BlogTitleUpdateRequest.java new file mode 100644 index 0000000..6fa58ef --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/service/dto/BlogTitleUpdateRequest.java @@ -0,0 +1,8 @@ +package org.sopt.spring.service.dto; + +import jakarta.validation.constraints.Size; + +public record BlogTitleUpdateRequest( + @Size(max = 10, message = "블로그 제목이 최대 글자 수(10자)를 초과했습니다.") String title +) { +} diff --git a/week03/src/main/java/org/sopt/spring/service/dto/MemberCreateDto.java b/week03/src/main/java/org/sopt/spring/service/dto/MemberCreateDto.java new file mode 100644 index 0000000..f4e84e9 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/service/dto/MemberCreateDto.java @@ -0,0 +1,10 @@ +package org.sopt.spring.service.dto; + +import org.sopt.spring.domain.Part; + +public record MemberCreateDto( + String name, + Part part, + int age +) { +} diff --git a/week03/src/main/java/org/sopt/spring/service/dto/MemberFindDto.java b/week03/src/main/java/org/sopt/spring/service/dto/MemberFindDto.java new file mode 100644 index 0000000..c50426a --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/service/dto/MemberFindDto.java @@ -0,0 +1,14 @@ +package org.sopt.spring.service.dto; + +import org.sopt.spring.domain.Member; +import org.sopt.spring.domain.Part; + +public record MemberFindDto( + String name, + Part part, + int age +) { + public static MemberFindDto of(Member member) { + return new MemberFindDto(member.getName(), member.getPart(), member.getAge()); + } +} diff --git a/week03/src/main/java/org/sopt/spring/service/dto/PostCreateRequest.java b/week03/src/main/java/org/sopt/spring/service/dto/PostCreateRequest.java new file mode 100644 index 0000000..4425dd5 --- /dev/null +++ b/week03/src/main/java/org/sopt/spring/service/dto/PostCreateRequest.java @@ -0,0 +1,8 @@ +package org.sopt.spring.service.dto; + +public record PostCreateRequest( + String title, + String content + +) {} + From e42cb1e0e8893e62c16787674fc208a4e6b3d5ff Mon Sep 17 00:00:00 2001 From: Yangdaehan Date: Thu, 2 May 2024 12:34:02 +0900 Subject: [PATCH 2/2] =?UTF-8?q?feat=20:=20POST=20=EB=90=98=EB=8A=94=20?= =?UTF-8?q?=EA=B0=9D=EC=B2=B4=EC=97=90=20=EB=8C=80=ED=95=B4=20Request=20bo?= =?UTF-8?q?dy=20Validation=EC=9D=84=20=EC=A7=84=ED=96=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/org/sopt/spring/controller/PostController.java | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/week03/src/main/java/org/sopt/spring/controller/PostController.java b/week03/src/main/java/org/sopt/spring/controller/PostController.java index 8ac1cf8..95a58d8 100644 --- a/week03/src/main/java/org/sopt/spring/controller/PostController.java +++ b/week03/src/main/java/org/sopt/spring/controller/PostController.java @@ -1,5 +1,6 @@ package org.sopt.spring.controller; +import jakarta.validation.Valid; import lombok.RequiredArgsConstructor; import org.sopt.spring.common.dto.SuccessStatusResponse; import org.sopt.spring.service.PostService; @@ -15,14 +16,14 @@ public class PostController { private final PostService postService; + @PostMapping("/posts") public ResponseEntity createPost( @RequestHeader Long blogId, - @RequestBody PostCreateRequest postCreateRequest) { + @Valid @RequestBody PostCreateRequest postCreateRequest) { return ResponseEntity.status(HttpStatus.CREATED).header( "Location", postService.create(blogId, postCreateRequest)) .body(SuccessStatusResponse.of(SuccessMessage.POST_CREATE_SUCCESS)); } } -