Skip to content

Commit

Permalink
afdSF
Browse files Browse the repository at this point in the history
  • Loading branch information
Yangdaehan committed May 1, 2024
1 parent faf0807 commit 62a4ffd
Show file tree
Hide file tree
Showing 31 changed files with 591 additions and 0 deletions.
Binary file added week03/src/main/java/.DS_Store
Binary file not shown.
Binary file added week03/src/main/java/org/.DS_Store
Binary file not shown.
Binary file added week03/src/main/java/org/sopt/.DS_Store
Binary file not shown.
13 changes: 13 additions & 0 deletions week03/src/main/java/org/sopt/spring/Application.java
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);
}

}
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 week03/src/main/java/org/sopt/spring/common/dto/ErrorMessage.java
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 week03/src/main/java/org/sopt/spring/common/dto/ErrorResponse.java
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());
}
}
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;
}
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());
}
}
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 {
}
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();
}
}
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();
}


}
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 week03/src/main/java/org/sopt/spring/domain/BaseTimeEntity.java
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;
}
45 changes: 45 additions & 0 deletions week03/src/main/java/org/sopt/spring/domain/Blog.java
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();
}
}
43 changes: 43 additions & 0 deletions week03/src/main/java/org/sopt/spring/domain/Member.java
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();
}
}
10 changes: 10 additions & 0 deletions week03/src/main/java/org/sopt/spring/domain/Part.java
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
}
Loading

0 comments on commit 62a4ffd

Please sign in to comment.