Skip to content

Commit

Permalink
Merge pull request #6 from GDSC-snowflowerthon/develop
Browse files Browse the repository at this point in the history
브랜치 merge
  • Loading branch information
Haewonny authored Jan 11, 2024
2 parents 21bdac1 + 440da73 commit d163243
Show file tree
Hide file tree
Showing 18 changed files with 516 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tenten.blooming.domain.goal.config;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class OpenAIRestTemplateConfig {

@Value("${openai.api.key}")
private String openaiApiKey;

@Bean
@Qualifier("openaiRestTemplate")
public RestTemplate openaiRestTemplate() {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getInterceptors().add((request, body, execution) -> {
request.getHeaders().add("Authorization", "Bearer " + openaiApiKey);
return execution.execute(request, body);
});
return restTemplate;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package tenten.blooming.domain.goal.controller;
import jakarta.persistence.Basic;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.client.RestTemplate;
import tenten.blooming.domain.goal.dto.ChatRequest;
import tenten.blooming.domain.goal.dto.ChatResponse;
import tenten.blooming.domain.goal.dto.GoalDto;
import tenten.blooming.domain.goal.entity.Goal;
import tenten.blooming.domain.goal.service.GoalService;
import tenten.blooming.global.common.BasicResponse;

@RestController
public class GoalController {

@Qualifier("openaiRestTemplate")
@Autowired
private RestTemplate restTemplate;

@Autowired
private GoalService goalService;

@Value("${openai.model}")
private String model;

@Value("${openai.api.url}")
private String apiUrl;

@PostMapping("/goal")
public ResponseEntity<BasicResponse> createGoal(@RequestBody GoalDto dto) {
Goal created = goalService.create(dto);
BasicResponse basicResponse = new BasicResponse();

if (created != null) {
basicResponse = BasicResponse.builder()
.code(HttpStatus.OK.value())
.message("목표 등록에 성공했습니다.")
.result(created)
.build();

}

return new ResponseEntity<>(basicResponse, HttpStatus.OK);

}

@GetMapping("/goal")
public String getSubGoal(@RequestParam String goalName) {
ChatRequest request = new ChatRequest(model, goalName);

ChatResponse response = restTemplate.postForObject(apiUrl, request, ChatResponse.class);

if (response == null || response.getChoices() == null || response.getChoices().isEmpty()) {
return "No response";
}

return response.getChoices().get(0).getMessage().getContent();
}
}
25 changes: 25 additions & 0 deletions src/main/java/tenten/blooming/domain/goal/dto/ChatRequest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package tenten.blooming.domain.goal.dto;

import lombok.*;

import java.util.ArrayList;
import java.util.List;

@Data
@Getter @Setter
@NoArgsConstructor
@AllArgsConstructor
public class ChatRequest {
private String model;
private List<Message> messages;
private final int n = 1;
private double temperature;

public ChatRequest(String model, String goalName) {
this.model = model;
this.messages = new ArrayList<>();
this.messages.add(new Message("system", "입력 받은 큰 목표 G를 달성하기 위한 구체적인 6가지 세부 목표 g1, g2, g3, g4, g5, g6 을 알려 주는 assistant bot. 6가지 세부 목표는 10일동안 매일매일 꾸준히 실천하여 10일 뒤에 G를 달성할 수 있는 목표여야 함."));
this.messages.add(new Message("user", "G: " + goalName));
}

}
23 changes: 23 additions & 0 deletions src/main/java/tenten/blooming/domain/goal/dto/ChatResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package tenten.blooming.domain.goal.dto;


import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.util.List;

@Data
@NoArgsConstructor
@Getter @Setter
public class ChatResponse {
private List<Choice> choices;

@Data
@NoArgsConstructor
public static class Choice {
private int idx;
private Message message;
}
}
27 changes: 27 additions & 0 deletions src/main/java/tenten/blooming/domain/goal/dto/GoalDto.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tenten.blooming.domain.goal.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.ToString;
import tenten.blooming.domain.goal.entity.Goal;

import java.time.LocalDate;

@Data
@AllArgsConstructor
@Builder
public class GoalDto {
private Long userId;
private String goalName;

public Goal toEntity() {
Goal goal = new Goal();

goal.setGoalName(goalName);
goal.setCreatedAt(LocalDate.now());
goal.setIsActivate(true);

return goal;
}
}
12 changes: 12 additions & 0 deletions src/main/java/tenten/blooming/domain/goal/dto/Message.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package tenten.blooming.domain.goal.dto;

import lombok.*;

@Data
@NoArgsConstructor
@AllArgsConstructor
@Getter @Setter
public class Message {
private String role;
private String content;
}
46 changes: 46 additions & 0 deletions src/main/java/tenten/blooming/domain/goal/entity/Goal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package tenten.blooming.domain.goal.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.springframework.data.annotation.CreatedDate;
import tenten.blooming.domain.subgoal.entity.Subgoal;
import tenten.blooming.domain.user.entity.User;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Getter
@Setter
@Table(name = "goal")
public class Goal {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "goal_id", nullable = false)
private Long goalId; // 기본키

@Column(name = "goal_name", nullable = false)
private String goalName;

@CreatedDate
@Column(name = "created_at", nullable = false, updatable = false)
private LocalDate createdAt;

@Column(name = "is_activate")
private Boolean isActivate;

@ManyToOne
@JoinColumn(name = "user_id")
private User user;

@OneToMany(mappedBy = "goal")
private List<Subgoal> subgoals = new ArrayList<>();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package tenten.blooming.domain.goal.repository;

import org.springframework.data.jpa.repository.JpaRepository;
import tenten.blooming.domain.goal.entity.Goal;

public interface GoalRepository extends JpaRepository<Goal, Long> {


}

19 changes: 19 additions & 0 deletions src/main/java/tenten/blooming/domain/goal/service/GoalService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package tenten.blooming.domain.goal.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tenten.blooming.domain.goal.dto.GoalDto;
import tenten.blooming.domain.goal.entity.Goal;
import tenten.blooming.domain.goal.repository.GoalRepository;

@Service
public class GoalService {
@Autowired
private GoalRepository goalRepository;

public Goal create(GoalDto dto) {
Goal goal = dto.toEntity();

return goalRepository.save(goal);
}
}
42 changes: 42 additions & 0 deletions src/main/java/tenten/blooming/domain/subgoal/entity/Subgoal.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package tenten.blooming.domain.subgoal.entity;

import jakarta.persistence.*;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import tenten.blooming.domain.goal.entity.Goal;

import java.time.LocalDate;

@AllArgsConstructor
@NoArgsConstructor
@Entity
@Getter
@Setter
@Table(name = "subgoal")
public class Subgoal {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "subgoal_id", nullable = false)
private Long subgoalId; // 기본키

@Column(name = "subgoal_name", nullable = false)
private String subgoalName;

private LocalDate doneDate1;
private LocalDate doneDate2;
private LocalDate doneDate3;
private LocalDate doneDate4;
private LocalDate doneDate5;
private LocalDate doneDate6;
private LocalDate doneDate7;
private LocalDate doneDate8;
private LocalDate doneDate9;
private LocalDate doneDate10;

@ManyToOne
@JoinColumn(name = "goal_id")
private Goal goal;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package tenten.blooming.domain.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
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 tenten.blooming.domain.user.entity.User;
import tenten.blooming.domain.user.dto.UserForm;
import tenten.blooming.domain.user.service.UserService;
import tenten.blooming.global.common.BasicResponse;

@RestController
@RequestMapping("/user")
public class UserController {

@Autowired
private UserService userService;

@PostMapping("/signup")
public ResponseEntity<BasicResponse> signup(@RequestBody UserForm dto) {
User user = userService.join(dto);
BasicResponse basicResponse = new BasicResponse();


if (user != null) {
basicResponse = BasicResponse.builder()
.code(HttpStatus.OK.value())
.message("회원가입에 성공하였습니다.")
.result(user)
.build();
}

return new ResponseEntity<>(basicResponse, HttpStatus.OK);
}

@PostMapping("/login")
public ResponseEntity<BasicResponse> login(@RequestBody UserForm dto) {
try {
User user = userService.login(dto.getLoginId(), dto.getPassword());

BasicResponse basicResponse = BasicResponse.builder()
.code(HttpStatus.OK.value())
.message("로그인에 성공하였습니다.")
.result(user)
.build();

return new ResponseEntity<>(basicResponse, HttpStatus.OK);
} catch (DataIntegrityViolationException e) {
BasicResponse basicResponse = BasicResponse.builder()
.code(HttpStatus.BAD_REQUEST.value())
.message("로그인에 실패했습니다.")
.result(null)
.build();

return new ResponseEntity<>(basicResponse, HttpStatus.BAD_REQUEST);
}
}
}
27 changes: 27 additions & 0 deletions src/main/java/tenten/blooming/domain/user/dto/UserForm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package tenten.blooming.domain.user.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import tenten.blooming.domain.user.entity.User;

@Data
@AllArgsConstructor
@Builder
public class UserForm {
private Long userId;

private String loginId;
private String nickname;
private String password;

public User toEntity() {
User user = new User();

user.setLoginId(loginId);
user.setNickname(nickname);
user.setPassword(password);

return user;
}
}
Loading

0 comments on commit d163243

Please sign in to comment.