Skip to content

Commit

Permalink
feat: 로그인, 투표하기, 프로젝트 내용 조회
Browse files Browse the repository at this point in the history
  • Loading branch information
ldw3097 committed Mar 30, 2024
1 parent 311285d commit 0a4b6db
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 9 deletions.
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
32 changes: 28 additions & 4 deletions src/main/java/byulbyul/byulbyulpoll/controller/PollController.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@
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 lombok.RequiredArgsConstructor;
Expand All @@ -18,10 +20,11 @@ public class PollController {

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

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

var pollInfoDtos = polls.stream()
Expand All @@ -33,7 +36,7 @@ public PollsResponseDto getPolls(){

@PostMapping
@Operation(summary = "투표 생성")
public MessageResponseDto createPoll(@RequestBody PollRequestDto pollRequestDto){
public MessageResponseDto createPoll(@RequestBody PollRequestDto pollRequestDto) {
MessageResponseDto response = new MessageResponseDto();
try {
pollService.createPoll(pollRequestDto.getTitle(), pollRequestDto.getStartDate(), pollRequestDto.getEndDate());
Expand All @@ -47,8 +50,8 @@ public MessageResponseDto createPoll(@RequestBody PollRequestDto pollRequestDto)
}

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

var projectInfoDtos = projects.stream()
Expand All @@ -58,7 +61,28 @@ public ProjectsResponseDto getPoll(@PathVariable long pollId){
return new ProjectsResponseDto(projectInfoDtos);
}

@GetMapping("/vote")
@Operation(summary = "투표하기")
public MessageResponseDto vote(@RequestParam long projectId,
@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
@@ -1,6 +1,7 @@
package byulbyul.byulbyulpoll.controller;

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;
Expand Down Expand Up @@ -29,4 +30,12 @@ public MessageResponseDto createProject(@RequestBody NewProjectDto newProjectDto
}
return response;
}

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

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
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;


public ProjectResponseDto(Long id, String title, String description, int voteCount){
this.id = id;
this.title = title;
this.description = description;
this.voteCount = voteCount;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package byulbyul.byulbyulpoll.repository;

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

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

public interface ProjectRepository extends JpaRepository<Project, Long> {

List<Project> findByPollId(long pollId);
List<Project> findByPollId(long pollId, Sort sort);

Optional<Project> findByTitle(String title);
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ public class MemberService {
private final PasswordEncoder passwordEncoder;

@Transactional
public void signUp(MemberDto memberDto) {
public Member signUp(MemberDto memberDto) {
if (isValidEmail(memberDto.getEmail()) && isValidNickname(memberDto.getNickname())
&& isValidBirthYear(memberDto.getBirthYear()) && isValidPassword(memberDto.getPassword())) {
memberDto.setPassword(passwordEncoder.encode(memberDto.getPassword()));
memberRepository.save(memberDto.toEntity());
return memberRepository.findByEmail(memberDto.getEmail()).orElseThrow();
}
return null;
}

public boolean isValidEmail(String email) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import byulbyul.byulbyulpoll.repository.ProjectRepository;
import byulbyul.byulbyulpoll.service.dto.NewProjectDto;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

Expand All @@ -32,7 +33,7 @@ public long createProject(NewProjectDto newprojectDto){
}

public List<Project> getProjects(long pollId){
return projectRepository.findByPollId(pollId);
return projectRepository.findByPollId(pollId, Sort.by(Sort.Direction.DESC, "id"));
}

public Project getProject(long projectId){
Expand Down

0 comments on commit 0a4b6db

Please sign in to comment.