Skip to content

Commit

Permalink
Merge pull request #71 from Team-MindWay/70-goal-get-api-create
Browse files Browse the repository at this point in the history
🔀 :: 현재 목표 가져오는 api
  • Loading branch information
Umjiseung authored Apr 13, 2024
2 parents cc9a464 + 535e743 commit e7ca736
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 7 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.mindway.server.v2.domain.Book.entity;
package com.mindway.server.v2.domain.book.entity;

import com.mindway.server.v2.domain.goal.entity.Goal;
import com.mindway.server.v2.domain.user.entity.User;
import jakarta.persistence.*;
import lombok.AllArgsConstructor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@
import jakarta.persistence.Embeddable;
import jakarta.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;

@Embeddable
@MappedSuperclass
@NoArgsConstructor
@AllArgsConstructor
@Getter
public class Week {

@Column(length = 3)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.mindway.server.v2.domain.goal.exception;

import com.mindway.server.v2.global.exception.ErrorCode;
import com.mindway.server.v2.global.exception.MindWayException;

public class NotExistGoalException extends MindWayException {
public NotExistGoalException() {
super(ErrorCode.EXIST_ALREADY_GOAL);
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,34 @@
package com.mindway.server.v2.domain.goal.presentation;

import com.mindway.server.v2.domain.goal.presentation.dto.request.GoalAddRequestDto;
import com.mindway.server.v2.domain.goal.presentation.dto.response.GoalGetResponse;
import com.mindway.server.v2.domain.goal.service.GoalAddService;
import com.mindway.server.v2.domain.goal.service.GoalGetService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v2/goal")
public class GoalController {

private final GoalAddService goalAddService;
private final GoalGetService goalGetService;

@PostMapping
public ResponseEntity<Void> addGoal(@Valid @RequestBody GoalAddRequestDto goalAddRequestDto) {
goalAddService.execute(goalAddRequestDto);
return ResponseEntity.status(HttpStatus.CREATED).build();
}

@GetMapping
public ResponseEntity<GoalGetResponse> getGoal() {
GoalGetResponse response = goalGetService.execute();
return ResponseEntity.ok(response);
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.mindway.server.v2.domain.goal.presentation.dto.response;

import com.mindway.server.v2.domain.goal.entity.Week;
import lombok.Builder;
import lombok.Getter;
import lombok.Setter;

@Getter
@Builder
@Setter
public class GoalGetResponse {

private Integer mon;

private Integer tue;

private Integer wed;

private Integer thu;

private Integer fri;

private Integer sat;

private Integer sun;

private Integer now_count;

private Integer goal_value;

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import com.mindway.server.v2.domain.user.entity.User;
import org.springframework.data.jpa.repository.JpaRepository;

import java.util.Optional;

public interface GoalRepository extends JpaRepository<Goal, Long> {
Boolean existsByUser(User user);
Optional<Goal> findByUser(User user);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.mindway.server.v2.domain.goal.service;

import com.mindway.server.v2.domain.goal.presentation.dto.response.GoalGetResponse;

public interface GoalGetService {
GoalGetResponse execute();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.mindway.server.v2.domain.goal.service.impl;

import com.mindway.server.v2.domain.goal.entity.Goal;
import com.mindway.server.v2.domain.goal.exception.NotExistGoalException;
import com.mindway.server.v2.domain.goal.presentation.dto.response.GoalGetResponse;
import com.mindway.server.v2.domain.goal.repository.GoalRepository;
import com.mindway.server.v2.domain.goal.service.GoalGetService;
import com.mindway.server.v2.domain.goal.util.GoalConverter;
import com.mindway.server.v2.domain.user.entity.User;
import com.mindway.server.v2.domain.user.util.UserUtil;
import com.mindway.server.v2.global.annotation.ServiceWithReadOnlyTransaction;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@ServiceWithReadOnlyTransaction
@RequiredArgsConstructor
@Slf4j
public class GoalGetServiceImpl implements GoalGetService {

private final GoalRepository goalRepository;
private final UserUtil userUtil;
private final GoalConverter goalConverter;

public GoalGetResponse execute() {
User user = userUtil.getCurrentUser();

Goal goal = goalRepository.findByUser(user)
.orElseThrow(NotExistGoalException::new);

return goalConverter.toDto(goal.getWeek(), goal.getGoal_value(), goal.getNow_count());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.mindway.server.v2.domain.goal.util;

import com.mindway.server.v2.domain.goal.entity.Goal;
import com.mindway.server.v2.domain.goal.entity.Week;
import com.mindway.server.v2.domain.goal.presentation.dto.response.GoalGetResponse;
import com.mindway.server.v2.domain.user.entity.User;

import java.time.LocalDate;

public interface GoalConverter {
Goal toEntity(User user, String created_at, String ended_at, Integer goal_count);
GoalGetResponse toDto(Week week, Integer goal_value, Integer now_count);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.mindway.server.v2.domain.goal.entity.Goal;
import com.mindway.server.v2.domain.goal.entity.Week;
import com.mindway.server.v2.domain.goal.presentation.dto.response.GoalGetResponse;
import com.mindway.server.v2.domain.goal.util.GoalConverter;
import com.mindway.server.v2.domain.user.entity.User;
import org.springframework.stereotype.Component;
Expand All @@ -21,5 +22,19 @@ public Goal toEntity(User user, String created_at, String ended_at, Integer goal
.user(user)
.build();
}

public GoalGetResponse toDto(Week week, Integer goal_value, Integer now_count) {
return GoalGetResponse.builder()
.mon(week.getMon())
.tue(week.getTue())
.wed(week.getWed())
.thu(week.getThu())
.fri(week.getFri())
.sat(week.getSat())
.sun(week.getSun())
.goal_value(goal_value)
.now_count(now_count)
.build();
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public enum ErrorCode {
NOT_ACCESS_STUDENT(403,"접근 권한이 없습니다."),

/* goal */
EXIST_ALREADY_GOAL(400, "이미 목표 설정이 되어있습니다.");
EXIST_ALREADY_GOAL(400, "이미 목표 설정이 되어있습니다."),
NOT_EXIST_GOAL(404, "유저가 설정한 목표가 존재하지 않습니다");

private final int status;
private final String message;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {

// goal
.requestMatchers(HttpMethod.POST, "/api/v2/goal").authenticated()
.requestMatchers(HttpMethod.GET, "/api/v2/goal").authenticated()

.anyRequest().authenticated()
)
Expand Down

0 comments on commit e7ca736

Please sign in to comment.