Skip to content

Commit

Permalink
Merge pull request #85 from BaeJunH0/dev
Browse files Browse the repository at this point in the history
[2025.01.01] dev 진행 내역 main에 머지
  • Loading branch information
BaeJunH0 authored Jan 1, 2025
2 parents 89e20be + 25ca985 commit 293ea6d
Show file tree
Hide file tree
Showing 23 changed files with 509 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.sparksInTheStep.webBoard.global.errorHandling.errorCode;

import lombok.AllArgsConstructor;
import lombok.Getter;
import org.springframework.http.HttpStatus;

@AllArgsConstructor
@Getter
public enum ProjectErrorCode implements ErrorCode{
NOT_FOUND(HttpStatus.NOT_FOUND, "Pr000", "No such project"),
NOT_TEAM_LEADER(HttpStatus.FORBIDDEN, "Pr001", "Project can be deleted by only team leader"),
ALREADY_EXIST_NAME(HttpStatus.FORBIDDEN, "Pr002", "Project's name is already using"),
ALREADY_JOINED_PROJECT(HttpStatus.FORBIDDEN, "Pr003", "Login user already in this project");

private final HttpStatus httpStatus;
private final String errorCode;
private final String message;

@Override
public HttpStatus httpStatus() {
return this.httpStatus;
}

@Override
public String code() {
return this.errorCode;
}

@Override
public String message() {
return this.message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ public class MemberService {
public boolean memberCheck(MemberCommand memberCommand){
if(isExistMember(memberCommand.nickname())){
Member savedMember = memberRepository.findByNickname(memberCommand.nickname());
Member checkMember = Member.of(memberCommand.nickname(), memberCommand.password());
Member checkMember = memberRepository.save(Member.of(
memberCommand.nickname(), memberCommand.password(), memberCommand.employed()
));

return savedMember.passCheck(checkMember.getPassword());
}
Expand All @@ -35,15 +37,29 @@ public void makeNewUser(MemberCommand memberCommand){
throw CustomException.of(MemberErrorCode.DUPLICATED_NICKNAME);
}

memberRepository.save(Member.of(memberCommand.nickname(), memberCommand.password()));
memberRepository.save(Member.of(
memberCommand.nickname(), memberCommand.password(), memberCommand.employed()
));
}

@Transactional
public void updateEmployed(String nickname) {
if(!isExistMember(nickname)){
throw CustomException.of(MemberErrorCode.NOT_FOUND);
}

Member member = memberRepository.findByNickname(nickname);
member.employing();
}

// 관리자용
@Transactional(readOnly = true)
public boolean adminCheck(MemberCommand memberCommand){
if(!isNotAdminMember(memberCommand.nickname())){
Member savedMember = memberRepository.findByNickname(memberCommand.nickname());
Member checkMember = Member.of(memberCommand.nickname(), memberCommand.password());
Member checkMember = memberRepository.save(Member.of(
memberCommand.nickname(), memberCommand.password(), memberCommand.employed()
));

return savedMember.passCheck(checkMember.getPassword());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import com.sparksInTheStep.webBoard.member.presentation.dto.MemberRequest;

public record MemberCommand(String nickname, String password) {
public record MemberCommand(String nickname, String password, Boolean employed) {
public static MemberCommand from(MemberRequest memberRequest){
return new MemberCommand(memberRequest.nickname(), memberRequest.password());
return new MemberCommand(
memberRequest.nickname(), memberRequest.password(), memberRequest.employed()
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.sparksInTheStep.webBoard.member.domain.Member;

public record MemberInfo() {
public record Default(String nickname){
public record Default(String nickname, Boolean employed){
public static MemberInfo.Default from(Member member){
return new MemberInfo.Default(member.getNickname());
return new MemberInfo.Default(member.getNickname(), member.isEmployed());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sparksInTheStep.webBoard.member.domain;

import com.sparksInTheStep.webBoard.project.doamin.Project;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
Expand All @@ -21,21 +22,28 @@ public class Member {
private UUID password;
@Column(nullable = false)
private boolean admin;
@Column(nullable = false)
private boolean employed;

private Member(String nickname, String password) {
private Member(String nickname, String password, Boolean employed) {
this.nickname = nickname;
this.password = encodePassword(password);
this.employed = employed;
this.admin = false;
}

public static Member of(String nickname, String password) {
return new Member(nickname, password);
public static Member of(String nickname, String password, Boolean employed) {
return new Member(nickname, password, employed);
}

public void granting() {
this.admin = !this.admin;
}

public void employing() {
this.employed = !this.employed;
}

public boolean passCheck(UUID password) {
return this.password.equals(password);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ public interface MemberApiSpec {
ResponseEntity<?> getNickname(
@AuthorizedUser MemberInfo.Default memberInfo
);

@Operation(summary = "유저 취업 여부 변경", description = "유저의 취업 유무를 변경합니다.")
@ApiResponse(responseCode="200", description = "성공")
ResponseEntity<?> updateEmployed(
@AuthorizedUser MemberInfo.Default memberInfo
);
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package com.sparksInTheStep.webBoard.member.presentation;

import com.sparksInTheStep.webBoard.global.annotation.AuthorizedUser;
import com.sparksInTheStep.webBoard.member.application.MemberService;
import com.sparksInTheStep.webBoard.member.application.dto.MemberInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@RequestMapping("/members")
@RequiredArgsConstructor
public class MemberController implements MemberApiSpec {
private final MemberService memberService;

@GetMapping("/my/nickname")
public ResponseEntity<?> getNickname(
@AuthorizedUser MemberInfo.Default memberInfo
) {
return new ResponseEntity<>(memberInfo.nickname(), HttpStatus.OK);
}

@PatchMapping("/my/employed")
public ResponseEntity<?> updateEmployed(
@AuthorizedUser MemberInfo.Default memberInfo
) {
memberService.updateEmployed(memberInfo.nickname());

return new ResponseEntity<>(HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package com.sparksInTheStep.webBoard.member.presentation.dto;

import jakarta.validation.constraints.NotBlank;

public record MemberRequest(
String nickname,

String password
String password,
Boolean employed
) {

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
import com.sparksInTheStep.webBoard.member.application.dto.MemberInfo;

public record MemberResponse() {
public record Default(String nickname){
public record Default(String nickname, Boolean employed){
public static MemberResponse.Default from(MemberInfo.Default memberInfo){
return new MemberResponse.Default(memberInfo.nickname());
return new MemberResponse.Default(memberInfo.nickname(), memberInfo.employed());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.sparksInTheStep.webBoard.post.domain;

public enum PostType{
INFORMATION,
FREE,
QUESTION
TIP,
PROJECT,
RESUME;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.sparksInTheStep.webBoard.project.application;

import com.sparksInTheStep.webBoard.global.errorHandling.CustomException;
import com.sparksInTheStep.webBoard.global.errorHandling.errorCode.ProjectErrorCode;
import com.sparksInTheStep.webBoard.project.doamin.JoinedProject;
import com.sparksInTheStep.webBoard.project.persistent.JoinedProjectRepository;
import com.sparksInTheStep.webBoard.member.domain.Member;
import com.sparksInTheStep.webBoard.member.persistent.MemberRepository;
import com.sparksInTheStep.webBoard.project.application.dto.ProjectCommand;
import com.sparksInTheStep.webBoard.project.application.dto.ProjectInfo;
import com.sparksInTheStep.webBoard.project.doamin.Project;
import com.sparksInTheStep.webBoard.project.persistent.ProjectRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@RequiredArgsConstructor
public class ProjectService {
private final ProjectRepository projectRepository;
private final MemberRepository memberRepository;
private final JoinedProjectRepository joinedProjectRepository;

@Transactional(readOnly = true)
public Page<ProjectInfo> getAllProjects(Pageable pageable){
return projectRepository.findAll(pageable).map(ProjectInfo::of);
}
@Transactional(readOnly = true)
public ProjectInfo getProject(Long id){
Project project = projectRepository.findById(id).orElseThrow(
() -> CustomException.of(ProjectErrorCode.NOT_FOUND)
);
return ProjectInfo.of(project);
}
@Transactional(readOnly = true)
public Page<ProjectInfo> getMyProjects(Pageable pageable, String nickname) {
Member member = memberRepository.findByNickname(nickname);
Page<JoinedProject> joinedProjects = joinedProjectRepository.findJoinedProjectsByMember(pageable, member);
return joinedProjects.map(JoinedProject::getProject).map(ProjectInfo::of);
}
@Transactional
public void makeProject(ProjectCommand projectCommand){
if(projectRepository.existsByName(projectCommand.name())) {
throw CustomException.of(ProjectErrorCode.ALREADY_EXIST_NAME);
}
Project project = Project.of(projectCommand);
projectRepository.save(project);
}
@Transactional
public void joinProject(Long id, String nickname) {
Project project = projectRepository.findById(id).orElseThrow(
() -> CustomException.of(ProjectErrorCode.NOT_FOUND)
);
Member member = memberRepository.findByNickname(nickname);

if(joinedProjectRepository.existsByMemberAndProject(member, project)){
throw CustomException.of(ProjectErrorCode.ALREADY_JOINED_PROJECT);
}
JoinedProject joinedProject = JoinedProject.from(member, project);
joinedProjectRepository.save(joinedProject);
}
@Transactional
public void updateProject(Long id, ProjectCommand projectCommand){
Project project = projectRepository.findById(id).orElseThrow(
() -> CustomException.of(ProjectErrorCode.NOT_FOUND)
);
if(projectRepository.existsByName(projectCommand.name())) {
throw CustomException.of(ProjectErrorCode.ALREADY_EXIST_NAME);
}
if(!project.getName().equals(projectCommand.nickname())) {
throw CustomException.of(ProjectErrorCode.NOT_TEAM_LEADER);
}

project.update(projectCommand.name(), projectCommand.info(), projectCommand.gitLink());
}

@Transactional
public void deleteProject(Long id, String nickname){
Project project = projectRepository.findById(id).orElseThrow(
() -> CustomException.of(ProjectErrorCode.NOT_FOUND)
);
if(!project.getName().equals(nickname)) {
throw CustomException.of(ProjectErrorCode.NOT_TEAM_LEADER);
}

projectRepository.delete(project);
}

@Transactional
public void withdrawProject(Long id, String nickname) {
Project project = projectRepository.findById(id).orElseThrow(
() -> CustomException.of(ProjectErrorCode.NOT_FOUND)
);
Member member = memberRepository.findByNickname(nickname);

if(!joinedProjectRepository.existsByMemberAndProject(member, project)){
throw CustomException.of(ProjectErrorCode.NOT_FOUND);
}

joinedProjectRepository.deleteByMemberAndProject(member, project);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sparksInTheStep.webBoard.project.application.dto;

import com.sparksInTheStep.webBoard.project.presentation.dto.ProjectRequest;

public record ProjectCommand(String name, String info, String gitLink, String nickname) {
public static ProjectCommand from(ProjectRequest projectRequest, String nickname) {
return new ProjectCommand(
projectRequest.name(), projectRequest.info(), projectRequest.gitLink(), nickname
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sparksInTheStep.webBoard.project.application.dto;

import com.sparksInTheStep.webBoard.project.doamin.Project;

public record ProjectInfo(String name, String info, String gitLink, String leaderName) {
public static ProjectInfo of(Project project){
return new ProjectInfo(
project.getName(), project.getInfo(), project.getGitLink(), project.getLeaderName()
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.sparksInTheStep.webBoard.project.doamin;

import com.sparksInTheStep.webBoard.member.domain.Member;
import jakarta.persistence.*;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.RequiredArgsConstructor;

@RequiredArgsConstructor(access = AccessLevel.PROTECTED)
@Entity
@Getter
public class JoinedProject {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "member_id")
private Member member;
@ManyToOne
@JoinColumn(name = "project_id")
private Project project;

private JoinedProject(Member member, Project project) {
this.member = member;
this.project = project;
}

public static JoinedProject from(
Member member, Project project
){
return new JoinedProject(member, project);
}
}
Loading

0 comments on commit 293ea6d

Please sign in to comment.