Skip to content

Commit

Permalink
feat: add some api of workspace (#2) (#4)
Browse files Browse the repository at this point in the history
* feat: add api of seeing missions of joined workspace

* feat: add api of checking duplication of workspace name

* fix: add resolver and validation

* feat: add api of entering workspace

* refactor: separate method
  • Loading branch information
aiaiaiai1 authored Jul 1, 2024
1 parent 0d07654 commit a8f2cd1
Show file tree
Hide file tree
Showing 13 changed files with 320 additions and 16 deletions.
22 changes: 16 additions & 6 deletions src/main/java/gymmi/controller/WorkspaceController.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import gymmi.request.JoiningWorkspaceRequest;
import gymmi.request.MatchingWorkspacePasswordRequest;
import gymmi.response.IdResponse;
import gymmi.response.InsideWorkspaceResponse;
import gymmi.response.JoinedWorkspaceResponse;
import gymmi.response.MatchingWorkspacePasswordResponse;
import gymmi.response.MissionResponse;
import gymmi.response.WorkspacePasswordResponse;
import gymmi.response.WorkspaceResponse;
import gymmi.service.WorkspaceService;
Expand Down Expand Up @@ -60,7 +62,7 @@ public ResponseEntity<WorkspacePasswordResponse> seeWorkspacePassword(

@GetMapping("/workspaces/{workspaceId}/match-password")
public ResponseEntity<MatchingWorkspacePasswordResponse> matchWorkspacePassword(
// @Logined User user, login intercept 추가
@Logined User user,
@PathVariable Long workspaceId,
@Validated @RequestBody MatchingWorkspacePasswordRequest request
) {
Expand All @@ -80,7 +82,7 @@ public ResponseEntity<List<JoinedWorkspaceResponse>> seeJoinedWorkspaces(

@GetMapping("/workspaces")
public ResponseEntity<List<WorkspaceResponse>> seeAllWorkspaces(
//@Logined User user,
@Logined User user,
@RequestParam(required = false) WorkspaceStatus status,
@RequestParam(required = false) String keyword,
@RequestParam(value = "page") int pageNumber
Expand Down Expand Up @@ -108,13 +110,21 @@ public ResponseEntity<Void> leaveWorkspace(
}

@GetMapping("/workspaces/{workspaceId}")
public ResponseEntity<Void> enterWorkspace() {
return null;
public ResponseEntity<InsideWorkspaceResponse> enterWorkspace(
@Logined User user,
@PathVariable Long workspaceId
) {
InsideWorkspaceResponse response = workspaceService.enterWorkspace(user, workspaceId);
return ResponseEntity.ok().body(response);
}

@GetMapping("/workspaces/{workspaceId}/missions")
public ResponseEntity<Void> seeMissionsInWorkspace() {
return null;
public ResponseEntity<List<MissionResponse>> seeMissionsInWorkspace(
@Logined User user,
@PathVariable Long workspaceId
) {
List<MissionResponse> responses = workspaceService.getMissionsInWorkspace(user, workspaceId);
return ResponseEntity.ok().body(responses);
}

@PostMapping("/workspaces/{workspaceId}/missions")
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/gymmi/entity/Mission.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,16 @@ public Mission(Workspace workspace, String name, Integer score) {
this.name = name;
this.score = score;
}

public Long getId() {
return id;
}

public String getName() {
return name;
}

public Integer getScore() {
return score;
}
}
18 changes: 17 additions & 1 deletion src/main/java/gymmi/entity/Worker.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class Worker {
private Long id;

@JoinColumn(name = "user_id", nullable = false)
@ManyToOne(fetch = FetchType.LAZY)
@ManyToOne(fetch = FetchType.EAGER)
private User user;

@JoinColumn(name = "workspace_id", nullable = false)
Expand All @@ -46,4 +46,20 @@ public Worker(User user, Workspace workspace) {
this.score = 0;
this.createdAt = LocalDateTime.now();
}

public Long getId() {
return id;
}

public User getUser() {
return user;
}

public String getNickname() {
return user.getNickname();
}

public Integer getScore() {
return score;
}
}
2 changes: 1 addition & 1 deletion src/main/java/gymmi/entity/Workspace.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public Workspace(
this.status = WorkspaceStatus.PREPARING;
}

private String validateName(String name) {
public static String validateName(String name) {
if (!REGEX_WORKSPACE_NAME.matcher(name).matches()) {
throw new InvalidPatternException("이름은 한글, 영문, 숫자만 가능합니다.");
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/gymmi/repository/MissionRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gymmi.repository;

import gymmi.entity.Mission;
import java.util.List;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
Expand All @@ -10,4 +11,7 @@ public interface MissionRepository extends JpaRepository<Mission, Long> {
@Modifying(clearAutomatically = true)
@Query("delete from Mission m where m.workspace.id = :workspaceId")
void deleteAllByWorkspaceId(Long workspaceId);

@Query("select m from Mission m where m.workspace.id = :workspaceId")
List<Mission> getAllByWorkspaceId(Long workspaceId);
}
4 changes: 4 additions & 0 deletions src/main/java/gymmi/repository/WorkerRepository.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package gymmi.repository;

import gymmi.entity.Worker;
import java.util.List;
import java.util.Optional;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
Expand All @@ -18,6 +19,9 @@ public interface WorkerRepository extends JpaRepository<Worker, Long> {
@Query("delete from Worker w where w.user.id = :userId and w.workspace.id = :workspaceId")
void deleteByUserIdAndWorkspaceId(Long userId, Long workspaceId);

@Query("select w from Worker w join w.workspace ws where ws.id = :workspaceId")
List<Worker> getAllByWorkspaceId(Long workspaceId);

// boolean으로 가능?? ->
// @Query(value = "select * from worker w", nativeQuery = true)
// boolean findByUserIdWorkspaceId(Long userId, Long workspaceId);
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/gymmi/response/InsideWorkspaceResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package gymmi.response;

import gymmi.entity.User;
import gymmi.entity.Worker;
import gymmi.entity.Workspace;
import java.util.ArrayList;
import java.util.List;
import lombok.Builder;
import lombok.Getter;

@Getter
public class InsideWorkspaceResponse {

private final String name;
private final Integer headCount;
private final String status;
private final Integer goalScore;
private final String description;
private final Integer achievementScore;
private final List<WorkerResponse> workers;

@Builder
public InsideWorkspaceResponse(
Workspace workspace, Integer achievementScore,
List<Worker> sortedWorkers, List<Integer> workerRanks,
User loginedUser
) {
this.name = workspace.getName();
this.headCount = workspace.getHeadCount();
this.status = workspace.getStatus().name();
this.goalScore = workspace.getGoalScore();
this.description = workspace.getDescription();
this.achievementScore = achievementScore;
this.workers = getWorkerResponse(sortedWorkers, workerRanks, workspace.getCreator(), loginedUser);
}

private List<WorkerResponse> getWorkerResponse(
List<Worker> sortedWorkers, List<Integer> workerRanks,
User creator, User loginedUser
) {
List<WorkerResponse> workerResponses = new ArrayList<>();
for (int i = 0; i < sortedWorkers.size(); i++) {
Worker worker = sortedWorkers.get(i);
WorkerResponse response = WorkerResponse.builder()
.worker(worker)
.rank(workerRanks.get(i))
.isCreator(creator.equals(worker.getUser()))
.isMyself(worker.getUser().equals(loginedUser))
.build();
workerResponses.add(response);
}
return workerResponses;
}

}
20 changes: 20 additions & 0 deletions src/main/java/gymmi/response/MissionResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package gymmi.response;

import gymmi.entity.Mission;
import lombok.Builder;
import lombok.Getter;

@Getter
public class MissionResponse {

private final Long id;
private final String mission;
private final Integer score;

public MissionResponse(Mission mission) {
this.id = mission.getId();
this.mission = mission.getName();
this.score = mission.getScore();
}

}
27 changes: 27 additions & 0 deletions src/main/java/gymmi/response/WorkerResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package gymmi.response;

import gymmi.entity.Worker;
import lombok.Builder;
import lombok.Getter;

@Getter
public class WorkerResponse {

private final Long id;
private final String name;
private final Integer contributeScore;
private final Integer rank;
private final Boolean isCreator;
private final Boolean isMyself;

@Builder
public WorkerResponse(Worker worker, Integer rank, boolean isCreator, boolean isMyself) {
this.id = worker.getId();
this.name = worker.getNickname();
this.contributeScore = worker.getScore();
this.rank = rank;
this.isCreator = isCreator;
this.isMyself = isMyself;
}

}
25 changes: 25 additions & 0 deletions src/main/java/gymmi/service/WorkspaceNameDuplicationCheck.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package gymmi.service;

import gymmi.entity.Workspace;
import gymmi.global.DuplicationCheckType;
import gymmi.repository.WorkspaceRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class WorkspaceNameDuplicationCheck implements DuplicationCheck {

private final WorkspaceRepository workspaceRepository;

@Override
public boolean supports(DuplicationCheckType type) {
return type == DuplicationCheckType.WORKSPACE_NAME;
}

@Override
public boolean isDuplicate(String value) {
Workspace.validateName(value);
return workspaceRepository.findWorkspaceByByName(value).isPresent();
}
}
Loading

0 comments on commit a8f2cd1

Please sign in to comment.