Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

πŸ”€ :: ν˜„μž¬ λͺ©ν‘œ κ°€μ Έμ˜€λŠ” api #71

Merged
merged 10 commits into from
Apr 13, 2024
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
Loading