Skip to content

Commit

Permalink
✨ 날씨 정보 요약, 최단 경로 조회, 추천 이력 조회 API, Github Actions 배포 프로세스 추가
Browse files Browse the repository at this point in the history
- [AJ-14] 설정파일 경로 classpath로 변경
  • Loading branch information
tinajeong committed Oct 28, 2024
1 parent 19d0aa5 commit 77664e1
Show file tree
Hide file tree
Showing 33 changed files with 472 additions and 22 deletions.
71 changes: 71 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
name: Deploy

on: []

env:
REPO: lyf-jarvis-back

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'corretto'


- name: Set Yaml
uses: microsoft/variable-substitution@v1
with:
files: ./src/main/resources/application-prod.yml
env:
spring.datasource.url: ${{ secrets.DB_HOST }}
spring.datasource.username: admin
spring.datasource.password: ${{ secrets.DB_PASSWORD }}

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Build with Gradle
run: ./gradlew build

- name: Log in to Docker Hub
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
context : .
file: Dockerfile
push: true
tags: ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPO }}:latest

deploy-to-ec2:
needs: build
runs-on: ubuntu-latest

steps:
- name: Deploy to EC2
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_HOST }}
username: ${{ secrets.EC2_SSH_USER }}
key: ${{ secrets.EC2_SSH_PRIVATE_KEY }}
script: |
CONTAINER_ID=$(sudo docker ps -q --filter "publish=8080-8081")
if [ ! -z "$CONTAINER_ID" ]; then
sudo docker stop $CONTAINER_ID || true
sudo docker rm $CONTAINER_ID || true
fi
sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/lyf-jarvis-back:latest
sudo docker run -d --name lyf-jarvis-back-${{ github.sha }} -p 8081:8080 ${{ secrets.DOCKERHUB_USERNAME }}/lyf-jarvis-back:latest
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
import jakarta.persistence.Column;
import jakarta.persistence.EntityListeners;
import jakarta.persistence.MappedSuperclass;
import lombok.Getter;

import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import java.time.LocalDateTime;

@Getter
@MappedSuperclass
@EntityListeners(AuditingEntityListener.class)
public abstract class BaseEntity {

@CreatedDate
@Column(name = "CREATED_DATE", updatable = false, nullable = false)
private LocalDateTime createdDate;
protected LocalDateTime createdDate;

@LastModifiedDate
@Column(name = "UPDATED_DATE")
private LocalDateTime updatedDate;
protected LocalDateTime updatedDate;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.moneygement.lyf.jarvis.history.controller;

import java.util.List;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
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 com.moneygement.lyf.jarvis.history.domain.History;
import com.moneygement.lyf.jarvis.history.domain.HistoryCreateRequest;
import com.moneygement.lyf.jarvis.history.domain.HistoryResponse;
import com.moneygement.lyf.jarvis.history.service.HistoryService;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;

@RestController
@RequestMapping("/api/history")
@RequiredArgsConstructor
@Tag(name = "History", description = "추천 목록 정보 조회 AP")
public class HistoryController {

private final HistoryService historyService;

@GetMapping("/{userId}")
@Operation(summary = "History 조회", description = "UserId 기준의 History 정보를 제공 합니다.")
public List<HistoryResponse> getHistoryByUserId(@PathVariable String userId) {
return historyService.findByUserId(userId);
}

@PostMapping
@Operation(summary = "History 등록", description = "History를 저장하는 기능을 제공합니다.")
public ResponseEntity<History> createHistory(@RequestBody HistoryCreateRequest request) {
History savedHistory = historyService.save(request);
return new ResponseEntity<>(savedHistory, HttpStatus.CREATED);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package com.moneygement.lyf.jarvis.history.domain;

import com.moneygement.lyf.jarvis.common.audit.BaseEntity;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Entity
@Setter
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "history")
public class History extends BaseEntity {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private Long id;

@Column(name = "user_id", nullable = false)
private String userId;

@Column(name = "group_id")
private Long groupId;

@Column(name = "request", length = 2500, nullable = false)
private String request;

@Column(name = "response", length = 2500, nullable = false)
private String response;

public History(String userId, Long groupId, String request, String response) {
this.userId = userId;
this.groupId = groupId;
this.request = request;
this.response = response;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.moneygement.lyf.jarvis.history.domain;

import jakarta.validation.constraints.NotNull;

public record HistoryCreateRequest(
@NotNull(message = "user id should be not null") String userId,
Long groupId,
@NotNull(message = "request should be not null") String request,
@NotNull(message = "response should be not null") String response
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.moneygement.lyf.jarvis.history.domain;

import java.time.LocalDateTime;

public record HistoryResponse(String userId,
Long groupId,
String jarvisMessage,
String userMessage,
LocalDateTime createdDate) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.moneygement.lyf.jarvis.history.persistence;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
import java.util.List;

import com.moneygement.lyf.jarvis.history.domain.History;

@Repository
public interface HistoryRepository extends JpaRepository<History, Long> {

// userId로 History 목록 조회
List<History> findByUserId(String userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.moneygement.lyf.jarvis.history.service;

import java.util.List;

import com.moneygement.lyf.jarvis.history.domain.History;
import com.moneygement.lyf.jarvis.history.domain.HistoryCreateRequest;
import com.moneygement.lyf.jarvis.history.domain.HistoryResponse;

public interface HistoryService {

History save(HistoryCreateRequest historyCreateRequest);

List<HistoryResponse> findByUserId(String userId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.moneygement.lyf.jarvis.history.service;

import java.util.List;
import java.util.Optional;

import org.springframework.stereotype.Service;

import com.moneygement.lyf.jarvis.history.domain.History;
import com.moneygement.lyf.jarvis.history.domain.HistoryCreateRequest;
import com.moneygement.lyf.jarvis.history.domain.HistoryResponse;
import com.moneygement.lyf.jarvis.history.persistence.HistoryRepository;
import com.moneygement.lyf.jarvis.user.domain.User;
import com.moneygement.lyf.jarvis.user.service.UserService;

import jakarta.persistence.EntityNotFoundException;
import lombok.RequiredArgsConstructor;

@Service
@RequiredArgsConstructor
public class HistoryServiceImpl implements HistoryService {

private final HistoryRepository historyRepository;
private final UserService userService;

@Override
public History save(HistoryCreateRequest request) {
validateUserId(request.userId());

return historyRepository.save(
new History(request.userId(),
request.groupId(),
request.request(),
request.response()));
}

@Override
public List<HistoryResponse> findByUserId(String userId) {
validateUserId(userId);
return historyRepository.findByUserId(userId).stream().map(
history -> new HistoryResponse(history.getUserId(),
history.getGroupId(),
history.getResponse(),
history.getRequest(),
history.getCreatedDate()
)
).toList();
}

/**
* UserId가 실제 존재하는지 확인
* @param userId
*/
private void validateUserId(String userId) {
Optional<User> user = userService.findById(userId);

if(user.isEmpty()) {
throw new EntityNotFoundException("user not found!");
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.moneygement.lyf.jarvis.recommendation.domain;

import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@Getter
@Setter
@Entity
@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Table(name = "group_recommendation")
public class GroupRecommendation {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "group_id")
private Long groupId;

@Column(name = "session_name", length = 100)
private String sessionName;

@Column(name = "introduction", length = 500)
private String introduction;

@Column(name = "owner_user_id", nullable = false, length = 40)
private String ownerUserId;

public GroupRecommendation(String sessionName, String introduction, String ownerUserId) {
this.sessionName = sessionName;
this.introduction = introduction;
this.ownerUserId = ownerUserId;
}
}

Loading

0 comments on commit 77664e1

Please sign in to comment.