Skip to content

과제 1-1 jihoonwjj 과제제출 #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: task/hjh
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version = '0.0.1-SNAPSHOT'

java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
languageVersion = JavaLanguageVersion.of(23)
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.gsm._8th.class4.backed.task._1._1.domain.controller;

import com.gsm._8th.class4.backed.task._1._1.domain.dto.AnggimoddiDTO;
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import com.gsm._8th.class4.backed.task._1._1.domain.service.DeletePost;
import com.gsm._8th.class4.backed.task._1._1.domain.service.GetPost;
import com.gsm._8th.class4.backed.task._1._1.domain.service.Postpost;
import com.gsm._8th.class4.backed.task._1._1.domain.service.UpdatePost;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@RestController
@RequestMapping("/articles")
@RequiredArgsConstructor
public class AnggimoddiController {

private final GetPost getPost;
private final Postpost postpost;
private final UpdatePost updatePost;
private final DeletePost deletePost;

@GetMapping("/")
public ResponseEntity<List<Ang>> getShitAll() {
return getPost.getAngs();
}

@GetMapping("/{id}")
public ResponseEntity<Optional<Ang>> getThatShit(@PathVariable Long id) {
return getPost.getAngById(id);
}

@PostMapping("/")
public ResponseEntity<Ang> makeShit(Ang ang) {
return postpost.postPost(ang);
}
Comment on lines +26 to +39
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GET /articlesPOST /articles 와 같은 상황에서는 ("/")로 명시하는 것보다 그냥 아무것도 안남기고 @PostMapping 이렇게만 하는게 관례에 더 맞을 것 같습니다


@PatchMapping(("/{id}"))
public ResponseEntity<Ang> editThatShit(AnggimoddiDTO anggimoddiDTO, @PathVariable Long id) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Vaild 어노테이션 적용 안하면 값 검증이 활성화가 안되요!

return updatePost.updatePost(anggimoddiDTO, id);
}

@DeleteMapping("/{id}")
public ResponseEntity<Ang> removeThatShit(@PathVariable Long id) {
return deletePost.deleteAng(id);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.gsm._8th.class4.backed.task._1._1.domain.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@NoArgsConstructor
@AllArgsConstructor
@Getter
@Builder
public class AnggimoddiDTO {
private Long id;
private String title;
private String content;
}
Comment on lines +8 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Size@Max 등 수많은 값 검증 어노테이션이 있으니까 특히 Request로 받는 DTO는 검증을 강화해줍시다

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.gsm._8th.class4.backed.task._1._1.domain.entity;

import jakarta.persistence.Entity;
import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Getter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class Ang extends BaseIdxEntity{
private String title;
private String content;

@Builder
public Ang(Long idx, String title, String content){
this.idx = idx;
this.title = title;
this.content = content;
}
}
Comment on lines +11 to +22
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image
클래스명 정상화 부탁드립니다

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.gsm._8th.class4.backed.task._1._1.global.entity;
package com.gsm._8th.class4.backed.task._1._1.domain.entity;

import jakarta.persistence.*;
import lombok.Getter;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.gsm._8th.class4.backed.task._1._1.domain.repository;

import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface AngRepository extends JpaRepository<Ang, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.gsm._8th.class4.backed.task._1._1.domain.service;

import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public interface DeletePost {
ResponseEntity<Ang> deleteAng(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.gsm._8th.class4.backed.task._1._1.domain.service;

import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
public interface GetPost {
ResponseEntity<List<Ang>> getAngs();
ResponseEntity<Optional<Ang>> getAngById(Long id);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.gsm._8th.class4.backed.task._1._1.domain.service;

import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 녀석 인터페이스에 @Service? 보통의 Aura가 아니군

public interface Postpost {
ResponseEntity<Ang> postPost(Ang ang);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.gsm._8th.class4.backed.task._1._1.domain.service;

import com.gsm._8th.class4.backed.task._1._1.domain.dto.AnggimoddiDTO;
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;

@Service
public interface UpdatePost {
ResponseEntity<Ang> updatePost(AnggimoddiDTO angimoddiDTO, Long postId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.gsm._8th.class4.backed.task._1._1.domain.service.impl;

import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import com.gsm._8th.class4.backed.task._1._1.domain.repository.AngRepository;
import com.gsm._8th.class4.backed.task._1._1.domain.service.DeletePost;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;

@RequiredArgsConstructor
public class DeletePostImpl implements DeletePost {
private final AngRepository angRepository;

public ResponseEntity<Ang> deleteAng(Long id) {
angRepository.deleteById(id);
return ResponseEntity.noContent().build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.gsm._8th.class4.backed.task._1._1.domain.service.impl;

import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import com.gsm._8th.class4.backed.task._1._1.domain.repository.AngRepository;
import com.gsm._8th.class4.backed.task._1._1.domain.service.GetPost;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

@RequiredArgsConstructor
public class GetPostImpl implements GetPost {
private final AngRepository angRepository;

public ResponseEntity<List<Ang>> getAngs() {
try {
return ResponseEntity.ok(angRepository.findAll());
} catch (IllegalArgumentException e) {
List<Ang> emptyList = Collections.emptyList();
return ResponseEntity.ok(emptyList);
}
}
Comment on lines +17 to +24
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

같이 협업하는 클라이언트 마다 다르긴 했는데 대부분의 클라이언트는 List형 API에서는 값이 없으면 그냥 Empty List를 반환해 주는 것을 선호하더라고요


public ResponseEntity<Optional<Ang>> getAngById(Long id) {
try {
return ResponseEntity.ok(angRepository.findById(id));
} catch (IllegalArgumentException e) {
return ResponseEntity.notFound().build();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.gsm._8th.class4.backed.task._1._1.domain.service.impl;

import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import com.gsm._8th.class4.backed.task._1._1.domain.repository.AngRepository;
import com.gsm._8th.class4.backed.task._1._1.domain.service.Postpost;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;

import java.util.Optional;

@RequiredArgsConstructor
public class PostpostImpl implements Postpost {
private final AngRepository angRepository;

public ResponseEntity<Ang> postPost(Ang ang) {
return ResponseEntity.ok(angRepository.save(ang));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이런 식으로 설계되어 있으면 오류 발생 시 500뜨고 말거나 이상한 값이 날라갈 거 같은데 예외 처리를 해주는건 어떨까요

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.gsm._8th.class4.backed.task._1._1.domain.service.impl;

import com.gsm._8th.class4.backed.task._1._1.domain.dto.AnggimoddiDTO;
import com.gsm._8th.class4.backed.task._1._1.domain.entity.Ang;
import com.gsm._8th.class4.backed.task._1._1.domain.repository.AngRepository;
import com.gsm._8th.class4.backed.task._1._1.domain.service.UpdatePost;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;


@Transactional(readOnly = true)
@RequiredArgsConstructor
public class UpdatePostImpl implements UpdatePost {
private final AngRepository angRepository;

public ResponseEntity<Ang> updatePost(AnggimoddiDTO anggimoddiDTO, Long postId) {
try {
Optional<Ang> ang = angRepository.findById(postId);
Ang upAng = ang.get();
upAng.builder()
.idx(anggimoddiDTO.getId())
.title(anggimoddiDTO.getTitle())
.content(anggimoddiDTO.getContent())
.build();
angRepository.save(upAng);
return ResponseEntity.ok(upAng);
} catch (IllegalArgumentException e) {
return ResponseEntity.notFound().build();
}
}
}