Skip to content
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

도서 추천 api #105

Merged
merged 9 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 7 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
30 changes: 30 additions & 0 deletions src/main/java/com/mindway/server/v2/domain/rec/entity/Rec.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.mindway.server.v2.domain.rec.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Table(name = "recommend")
public class Rec {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

private String title;

private String content;

private String author;

@Enumerated(EnumType.STRING)
private Type type;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mindway.server.v2.domain.rec.entity;

public enum Type {
NOVEL,
ESSAY
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.mindway.server.v2.domain.rec.presentation;

import com.mindway.server.v2.domain.rec.entity.Type;
import com.mindway.server.v2.domain.rec.presentation.dto.reqest.WriteRecRequest;
import com.mindway.server.v2.domain.rec.service.WriteRecService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2/recommend")
public class RecController {
private final WriteRecService writeRecService;

@PostMapping
public ResponseEntity<Void> writeRecBook (@RequestBody @Valid WriteRecRequest writeRecRequest, @RequestParam Type type){
writeRecService.execute(writeRecRequest, type);
return ResponseEntity.status(HttpStatus.CREATED).build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.mindway.server.v2.domain.rec.presentation.dto.reqest;

import jakarta.validation.constraints.NotBlank;
import lombok.Builder;
import lombok.Getter;

@Builder
@Getter
public class WriteRecRequest {
@NotBlank
private String title;

@NotBlank
private String content;

@NotBlank
private String author;
}
Copy link
Member

Choose a reason for hiding this comment

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

제가 알기론 숫자가 들어가면 @NotBlank가 적합하지 않는 걸로 아는데 맞나요?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

문자열이라 상관없는거 같아요

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mindway.server.v2.domain.rec.repository;

import com.mindway.server.v2.domain.rec.entity.Rec;
import org.springframework.data.jpa.repository.JpaRepository;

public interface RecRepository extends JpaRepository<Rec, Long> {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.mindway.server.v2.domain.rec.service;

import com.mindway.server.v2.domain.rec.entity.Type;
import com.mindway.server.v2.domain.rec.presentation.dto.reqest.WriteRecRequest;

public interface WriteRecService {
void execute(WriteRecRequest writeRecRequest, Type type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.mindway.server.v2.domain.rec.service.impl;

import com.mindway.server.v2.domain.order.exception.NotAccessStudentException;
import com.mindway.server.v2.domain.rec.entity.Rec;
import com.mindway.server.v2.domain.rec.entity.Type;
import com.mindway.server.v2.domain.rec.presentation.dto.reqest.WriteRecRequest;
import com.mindway.server.v2.domain.rec.repository.RecRepository;
import com.mindway.server.v2.domain.rec.service.WriteRecService;
import com.mindway.server.v2.domain.rec.util.RecConverter;
import com.mindway.server.v2.domain.user.entity.Authority;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.util.UserUtil;
import com.mindway.server.v2.global.annotation.ServiceWithTransaction;
import lombok.RequiredArgsConstructor;

@ServiceWithTransaction
@RequiredArgsConstructor
public class WriteRecServiceImpl implements WriteRecService {

private final RecRepository recRepository;
private final RecConverter recConverter;
private final UserUtil userUtil;

public void execute(WriteRecRequest writeRecRequest, Type type) {
User user = userUtil.getCurrentUser();

if (user.getAuthority() == Authority.ROLE_STUDENT) {
throw new NotAccessStudentException();
}

recRepository.save(recConverter.toEntity(writeRecRequest, type));
}
}
Copy link
Member

Choose a reason for hiding this comment

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

저희 코드 스타일이 컨버터로 변환하고 그걸 객체를 따로 만들어서 save하는 느낌인걸로 알고 있어요
코드를 일관성있게 하면 좋을 거 같아요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

01ac3a2
수정했습니다

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.mindway.server.v2.domain.rec.util;

import com.mindway.server.v2.domain.rec.entity.Rec;
import com.mindway.server.v2.domain.rec.entity.Type;
import com.mindway.server.v2.domain.rec.presentation.dto.reqest.WriteRecRequest;

public interface RecConverter {
Rec toEntity (WriteRecRequest writeRecRequest, Type type);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.mindway.server.v2.domain.rec.util.impl;

import com.mindway.server.v2.domain.rec.entity.Rec;
import com.mindway.server.v2.domain.rec.entity.Type;
import com.mindway.server.v2.domain.rec.presentation.dto.reqest.WriteRecRequest;
import com.mindway.server.v2.domain.rec.util.RecConverter;
import org.springframework.stereotype.Component;

@Component
public class RecConverterImpl implements RecConverter {

public Rec toEntity(WriteRecRequest writeRecRequest, Type type) {
return Rec.builder()
.title(writeRecRequest.getTitle())
.content(writeRecRequest.getContent())
.author(writeRecRequest.getAuthor())
.type(type)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
// rank
.requestMatchers(HttpMethod.GET, "/api/v2/rank").authenticated()

// recommend
.requestMatchers(HttpMethod.POST, "/api/v2/recommend").hasAnyAuthority(Authority.ROLE_TEACHER.name(), Authority.ROLE_STUDENT.name())

Copy link
Member

@Umjiseung Umjiseung Apr 23, 2024

Choose a reason for hiding this comment

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

권한 허용이 잘못 되어있는거같은데요? 수정부탁드려요

Copy link
Contributor Author

Choose a reason for hiding this comment

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

e36184f
수정했습니다

.anyRequest().authenticated()
)

Expand Down
Loading