-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
faf0807
commit 62a4ffd
Showing
31 changed files
with
591 additions
and
0 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
|
||
} |
27 changes: 27 additions & 0 deletions
27
week03/src/main/java/org/sopt/spring/common/GlobalExceptionHandler.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<ErrorResponse> 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<ErrorResponse> handleNotFoundException(NotFoundException e) { | ||
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(ErrorResponse.of(e.getErrorMessage())); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
week03/src/main/java/org/sopt/spring/common/dto/ErrorMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
14 changes: 14 additions & 0 deletions
14
week03/src/main/java/org/sopt/spring/common/dto/ErrorResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
week03/src/main/java/org/sopt/spring/common/dto/SuccessMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
11 changes: 11 additions & 0 deletions
11
week03/src/main/java/org/sopt/spring/common/dto/SuccessStatusResponse.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
week03/src/main/java/org/sopt/spring/config/JpaAuditingConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 { | ||
} |
46 changes: 46 additions & 0 deletions
46
week03/src/main/java/org/sopt/spring/controller/BlogController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SuccessStatusResponse> 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(); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
week03/src/main/java/org/sopt/spring/controller/MemberController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<MemberFindDto> 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(); | ||
} | ||
|
||
|
||
} |
28 changes: 28 additions & 0 deletions
28
week03/src/main/java/org/sopt/spring/controller/PostController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<SuccessStatusResponse> 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)); | ||
} | ||
} | ||
|
21 changes: 21 additions & 0 deletions
21
week03/src/main/java/org/sopt/spring/domain/BaseTimeEntity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package org.sopt.spring.domain; | ||
|
||
public enum Part { | ||
IOS, | ||
SERVER, | ||
ANDROID, | ||
WEB, | ||
PLAN, | ||
DESIGN | ||
} |
Oops, something went wrong.