Skip to content

Commit

Permalink
#42 [Feat] 자격증 추가 최종
Browse files Browse the repository at this point in the history
  • Loading branch information
dlguszoo committed Jan 24, 2024
1 parent a0247a1 commit c4ff8ee
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 34 deletions.
26 changes: 15 additions & 11 deletions src/main/java/umc/forgrad/controller/CertificateController.java
Original file line number Diff line number Diff line change
@@ -1,28 +1,32 @@
package umc.forgrad.controller;

import jakarta.validation.Valid;
import jakarta.websocket.server.PathParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
import umc.forgrad.apipayload.ApiResponse;
import umc.forgrad.converter.CertificateConverter;
import umc.forgrad.domain.Certificate;
import umc.forgrad.dto.CertificateRequestDto;
import umc.forgrad.dto.CertificateResponseDto;
import umc.forgrad.dto.AddCertificateRequestDto;
import umc.forgrad.dto.AddCertificateResponseDto;
import umc.forgrad.dto.ViewCertificateResponseDto;
import umc.forgrad.service.CertificateService;

import java.util.List;

@RestController
@RequiredArgsConstructor
public class CertificateController {
private final CertificateService certificateService;

@PostMapping(value = "/plans/certifications/stuId/{stuId}")
public ApiResponse<CertificateResponseDto> addCertificate(@RequestBody CertificateRequestDto certificateRequestDto,@PathParam("stuId") String stuId) {
Certificate certificate = certificateService.addCertificate(certificateRequestDto, Long.parseLong(stuId));
public ApiResponse<AddCertificateResponseDto> addCertificate(@RequestBody AddCertificateRequestDto addCertificateRequestDto, @PathVariable Long stuId) {
Certificate certificate = certificateService.addCertificate(addCertificateRequestDto, stuId);
return ApiResponse.onSuccess(CertificateConverter.toAddResultDto(certificate));
}


/*
@GetMapping(value = "/plans/certifications/stuId/{stuId}")
public ApiResponse<ViewCertificateResponseDto> viewCertificate(@PathVariable Long stuId) {
List<Certificate> = certificateService.viewCertificate(stuId);
return ApiResponse.onSuccess(CertificateConverter.toViewResultDto(certificate));
}
*/
}
23 changes: 16 additions & 7 deletions src/main/java/umc/forgrad/converter/CertificateConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,32 @@

import umc.forgrad.domain.Certificate;
import umc.forgrad.domain.Student;
import umc.forgrad.dto.CertificateRequestDto;
import umc.forgrad.dto.CertificateResponseDto;
import umc.forgrad.dto.AddCertificateRequestDto;
import umc.forgrad.dto.AddCertificateResponseDto;
import umc.forgrad.dto.ViewCertificateResponseDto;

import java.time.LocalDateTime;

public class CertificateConverter {
public static CertificateResponseDto toAddResultDto(Certificate certificate) {
return CertificateResponseDto.builder()
public static AddCertificateResponseDto toAddResultDto(Certificate certificate) {
return AddCertificateResponseDto.builder()
.certificateId(certificate.getId())
.createdAt(LocalDateTime.now())
.build();
}

public static Certificate toCertificate(CertificateRequestDto certificateRequestDto, Student student) {
/*
public static ViewCertificateResponseDto toViewResultDto(Certificate certificate) {
return ViewCertificateResponseDto.builder()
.name(certificate.getName())
.date(certificate.getDate())
.build();
}
*/
public static Certificate toCertificate(AddCertificateRequestDto addCertificateRequestDto, Student student) {
return Certificate.builder()
.name(certificateRequestDto.getName())
.date(certificateRequestDto.getDate())
.name(addCertificateRequestDto.getName())
.date(addCertificateRequestDto.getDate())
.student(student)
.build();
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/umc/forgrad/domain/Certificate.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import jakarta.persistence.*;
import lombok.*;
import umc.forgrad.domain.common.BaseEntity;
import umc.forgrad.dto.CertificateRequestDto;

import java.time.LocalDate;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
package umc.forgrad.dto;

import lombok.AccessLevel;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import umc.forgrad.domain.Certificate;
import umc.forgrad.domain.Student;

import java.time.LocalDate;

@Getter
@NoArgsConstructor
public class CertificateRequestDto {
public class AddCertificateRequestDto {
private String name;
private LocalDate date;

@Builder
public CertificateRequestDto(String name, LocalDate date) {
this.name = name;
this.date = date;
public AddCertificateRequestDto(Certificate certificate) {
this.name = certificate.getName();
this.date = certificate.getDate();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import umc.forgrad.domain.Certificate;

import java.time.LocalDateTime;

@Builder
@Getter
@NoArgsConstructor
@AllArgsConstructor
public class CertificateResponseDto {
public class AddCertificateResponseDto {
Long certificateId;
LocalDateTime createdAt;
}
19 changes: 12 additions & 7 deletions src/main/java/umc/forgrad/service/CertificateService.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,30 @@
import umc.forgrad.converter.CertificateConverter;
import umc.forgrad.domain.Certificate;
import umc.forgrad.domain.Student;
import umc.forgrad.dto.CertificateRequestDto;
import umc.forgrad.dto.AddCertificateRequestDto;
import umc.forgrad.repository.CertificateRepository;
import umc.forgrad.repository.StudentRepository;

import java.util.List;

@RequiredArgsConstructor
@Service
public class CertificateService {
private final CertificateRepository certificateRepository;
private final StudentRepository studentRepository;

@Transactional
public Certificate addCertificate(CertificateRequestDto certificateRequestDto, Long stuId) {
public Certificate addCertificate(AddCertificateRequestDto addCertificateRequestDto, Long stuId) {
Student student = studentRepository.findById(stuId)
.orElseThrow(() -> new IllegalArgumentException("해당 학번이 존재하지 않습니다. id=" + stuId));
return certificateRepository.save(CertificateConverter.toCertificate(certificateRequestDto, student));
return certificateRepository.save(CertificateConverter.toCertificate(addCertificateRequestDto, student));
}

/*public Certificate viewCertificate(Long stuId) {
return certificateRepository.findById();
}*/
/*
public List<Certificate> viewCertificate(Long stuId) {
Student student = studentRepository.findById(stuId)
.orElseThrow(() -> new IllegalArgumentException("해당 학번이 존재하지 않습니다. id=" + stuId));
return certificateRepository.findAllById();
}
*/
}

0 comments on commit c4ff8ee

Please sign in to comment.