Skip to content

Commit

Permalink
Merge branch 'feat/ncloud' of https://github.com/403project/back-end
Browse files Browse the repository at this point in the history
…into feat/ncloud
  • Loading branch information
jaewoong2 committed Mar 31, 2024
2 parents 0e2ae8b + 03f9dce commit cdd3988
Show file tree
Hide file tree
Showing 20 changed files with 300 additions and 33 deletions.
12 changes: 9 additions & 3 deletions src/main/java/byulbyul/byulbyulpoll/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,29 @@
import io.swagger.v3.oas.models.servers.Server;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;

import java.util.List;

@Configuration
public class SwaggerConfig {
@Bean
@Profile("real")
public OpenAPI openAPI() {

Server server = new Server();
server.setUrl("https://api.byulbyul.store/");
Server server1 = new Server();
server1.setUrl("https://api.byulbyul.store/");

Server server2 = new Server();
server2.setUrl("http://localhost:8080/");

return new OpenAPI()
.components(new Components())
.info(apiInfo())
.servers(List.of(server));
.servers(List.of(server1, server2));
}


private Info apiInfo() {
return new Info()
.title("ByulByulPoll API Test")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,11 @@ public MessageResponseDto isUniqueNickname(@RequestParam String nickname) {

@Operation(summary = "회원가입")
@PostMapping("/sign-up")
public MessageResponseDto signUp(@RequestBody MemberRequestDto memberRequestDto) {
public MessageResponseDto signUp(@RequestBody MemberRequestDto memberRequestDto, HttpSession session) {
MessageResponseDto response = new MessageResponseDto();
try {
memberService.signUp(memberRequestDto.toDto());
var member = memberService.signUp(memberRequestDto.toDto());
session.setAttribute("member", member);
response.setSuccess(true);
response.setMessage("회원가입에 성공했습니다.");
} catch (IllegalArgumentException e) {
Expand Down
74 changes: 66 additions & 8 deletions src/main/java/byulbyul/byulbyulpoll/controller/PollController.java
Original file line number Diff line number Diff line change
@@ -1,31 +1,89 @@
package byulbyul.byulbyulpoll.controller;

import byulbyul.byulbyulpoll.controller.dto.MessageResponseDto;
import byulbyul.byulbyulpoll.controller.dto.PollRequestDto;
import byulbyul.byulbyulpoll.controller.dto.PollsResponseDto;
import byulbyul.byulbyulpoll.controller.dto.ProjectsResponseDto;
import byulbyul.byulbyulpoll.entity.Member;
import byulbyul.byulbyulpoll.service.PollService;
import byulbyul.byulbyulpoll.service.ProjectService;
import byulbyul.byulbyulpoll.service.VoteService;
import byulbyul.byulbyulpoll.service.dto.NewProjectDto;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import jakarta.servlet.http.HttpSession;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController("/polls")
@RestController
@RequestMapping("/polls")
@RequiredArgsConstructor
public class PollController {

private final PollService pollService;
private final ProjectService projectService;
private final VoteService voteService;

@GetMapping
public PollsResponseDto getPolls(){
@Operation(summary = "투표 목록 조회")
public PollsResponseDto getPolls() {
var polls = pollService.getPolls();

var pollInfoDtos = polls.stream()
.map(poll -> new PollsResponseDto.PollInfoDto(poll.getTitle(), poll.isOngoing()))
.map(poll -> new PollsResponseDto.PollInfoDto(poll.getId(), poll.getTitle(), poll.isOngoing(), poll.getStartDate(), poll.getEndDate()))
.toArray(PollsResponseDto.PollInfoDto[]::new);

return new PollsResponseDto(pollInfoDtos);
}

// @PostMapping
// MessageResponseDto createPoll
@PostMapping
@Operation(summary = "투표 생성")
public MessageResponseDto createPoll(@RequestBody PollRequestDto pollRequestDto) {
MessageResponseDto response = new MessageResponseDto();
try {
pollService.createPoll(pollRequestDto.getTitle(), pollRequestDto.getStartDate(), pollRequestDto.getEndDate());
response.setSuccess(true);
response.setMessage("투표 생성에 성공했습니다.");
} catch (IllegalArgumentException e) {
response.setSuccess(false);
response.setMessage(e.getMessage());
}
return response;
}

@GetMapping("/{pollId}")
@Operation(summary = "투표 내부의 프로젝트 조회")
public ProjectsResponseDto getPoll(@PathVariable long pollId) {
var projects = projectService.getProjects(pollId);

var projectInfoDtos = projects.stream()
.map(project -> new ProjectsResponseDto.ProjectInfoDto(project.getId(), project.getTitle(), project.getDescription(), project.getVoteCount()))
.toArray(ProjectsResponseDto.ProjectInfoDto[]::new);

return new ProjectsResponseDto(projectInfoDtos);
}

@GetMapping("/vote")
@Operation(summary = "투표하기")
public MessageResponseDto vote(@RequestParam long projectId,
@Parameter(hidden = true) @SessionAttribute(name = "member", required = false) Member member) {
MessageResponseDto response = new MessageResponseDto();
if (member == null) {
response.setSuccess(false);
response.setMessage("로그인이 필요합니다. (비회원 투표 개발중)");
return response;
}

try {
voteService.voteByMember(projectId, member.getEmail());
response.setSuccess(true);
response.setMessage("투표에 성공했습니다.");
} catch (IllegalArgumentException e) {
response.setSuccess(false);
response.setMessage(e.getMessage());
}
return response;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package byulbyul.byulbyulpoll.controller;

import byulbyul.byulbyulpoll.controller.dto.AttachImageRequestDto;
import byulbyul.byulbyulpoll.controller.dto.MessageResponseDto;
import byulbyul.byulbyulpoll.controller.dto.ProjectResponseDto;
import byulbyul.byulbyulpoll.service.ProjectService;
import byulbyul.byulbyulpoll.service.dto.NewProjectDto;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/projects")
public class ProjectController {

private final ProjectService projectService;

@PostMapping
@Operation(summary = "프로젝트 생성")
public MessageResponseDto createProject(@RequestBody NewProjectDto newProjectDto){
MessageResponseDto response = new MessageResponseDto();
try {
projectService.createProject(newProjectDto);
response.setSuccess(true);
response.setMessage("프로젝트 생성에 성공했습니다.");
} catch (IllegalArgumentException e) {
response.setSuccess(false);
response.setMessage(e.getMessage());
}
return response;
}

@GetMapping("/{projectId}")
@Operation(summary = "프로젝트 조회")
public ProjectResponseDto getProject(@PathVariable long projectId){
var project = projectService.getProject(projectId);
var imageUrls = projectService.getProjectImages(projectId);
return new ProjectResponseDto(project.getId(), project.getTitle(), project.getDescription(), project.getVoteCount(), imageUrls);
}

@PostMapping("/{projectId}/images")
@Operation(summary = "이미지 첨부")
public MessageResponseDto addProjectImages(@PathVariable long projectId, @RequestBody AttachImageRequestDto requestDto){
MessageResponseDto response = new MessageResponseDto();
try {
projectService.addProjectImages(projectId, requestDto.getImageUrls());
response.setSuccess(true);
response.setMessage("이미지 첨부에 성공했습니다.");
} catch (IllegalArgumentException e) {
response.setSuccess(false);
response.setMessage(e.getMessage());
}
return response;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package byulbyul.byulbyulpoll.controller.dto;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.List;

@Data
@NoArgsConstructor
public class AttachImageRequestDto {
private List<String> imageUrls;

public AttachImageRequestDto(List<String> imageUrls) {
this.imageUrls = imageUrls;
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package byulbyul.byulbyulpoll.controller.dto;

import lombok.AllArgsConstructor;
import lombok.Data;

import java.time.LocalDateTime;

@Data
@AllArgsConstructor
public class PollRequestDto {

private String title;
private LocalDateTime startDate;
private LocalDateTime endDate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.cglib.core.Local;

import java.time.LocalDateTime;

@Data
public class PollsResponseDto {
Expand All @@ -13,12 +16,18 @@ public PollsResponseDto(PollInfoDto[] polls){

@Data
public static class PollInfoDto{
private String pollChannel;
private Long id;
private String title;
private boolean ongoing;
private LocalDateTime startDate;
private LocalDateTime endDate;

public PollInfoDto(String pollChannel, boolean ongoing){
this.pollChannel = pollChannel;
public PollInfoDto(Long id, String title, boolean ongoing, LocalDateTime startDate, LocalDateTime endDate){
this.id = id;
this.title = title;
this.ongoing = ongoing;
this.startDate = startDate;
this.endDate = endDate;
}

}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package byulbyul.byulbyulpoll.controller.dto;

import lombok.Data;

import java.util.List;

@Data
public class ProjectResponseDto {
private Long id;
private String title;
private String description;
private int voteCount;
private List<String> imageUrls;


public ProjectResponseDto(Long id, String title, String description, int voteCount, List<String> imageUrls){
this.id = id;
this.title = title;
this.description = description;
this.voteCount = voteCount;
this.imageUrls = imageUrls;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package byulbyul.byulbyulpoll.controller.dto;

import lombok.Data;

@Data
public class ProjectsResponseDto {
ProjectInfoDto[] projects;

public ProjectsResponseDto(ProjectInfoDto[] projects){
this.projects = projects;
}

@Data
public static class ProjectInfoDto{
private Long id;
private String title;
private String description;
private int voteCount;

public ProjectInfoDto(Long id, String title, String description, int voteCount){
this.id = id;
this.title = title;
this.description = description;
this.voteCount = voteCount;
}

}
}
7 changes: 7 additions & 0 deletions src/main/java/byulbyul/byulbyulpoll/entity/Project.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ public Project() {
public void upvote() {
this.voteCount++;
}

public void cancleUpvote() {
this.voteCount--;
if (this.voteCount < 0 ){
this.voteCount = 0;
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/byulbyul/byulbyulpoll/entity/ProjectImage.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,28 @@

import jakarta.persistence.*;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Getter
@Setter
@NoArgsConstructor
public class ProjectImage {
@Id
@GeneratedValue
private Long id;

private String imageUrl;

@ManyToOne(fetch = FetchType.LAZY)
private Project project;

private int imageOrder;

public ProjectImage(Project project, String imageUrl,int imageOrder ){
this.project = project;
this.imageUrl = imageUrl;
this.imageOrder = imageOrder;
}
}
4 changes: 2 additions & 2 deletions src/main/java/byulbyul/byulbyulpoll/entity/Vote.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ public class Vote {

private boolean isMember;

@OneToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY)
private Member member;

@OneToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.LAZY)
private NonMember nonMember;

private Integer ageType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
package byulbyul.byulbyulpoll.repository;

import byulbyul.byulbyulpoll.entity.ProjectImage;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.List;

public interface ProjectImageRepository extends JpaRepository<ProjectImage, Long> {

List<ProjectImage> findByProjectId(Long projectId, Sort sort);

}
Loading

0 comments on commit cdd3988

Please sign in to comment.