Skip to content

Commit

Permalink
Merge pull request #350 from Team-Smeme/feature/#349
Browse files Browse the repository at this point in the history
feature: AI코칭 기능 업데이트 및 몽고디비 연동
  • Loading branch information
thguss authored Oct 29, 2024
2 parents 857940a + cdb01b6 commit 778381d
Show file tree
Hide file tree
Showing 26 changed files with 265 additions and 192 deletions.
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ include 'smeem-input-http'
include 'smeem-application'
include 'smeem-output-persistence'
include 'smeem-output-persistence:postgresql'
include 'smeem-output-persistence:mongodb'
include 'smeem-output-notification'
include 'smeem-output-notification:firebase'
include 'smeem-output-oauth'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.smeem.application.port.input.dto.response.auth.GenerateTokenResponse;
import com.smeem.application.port.input.dto.response.auth.SignInResponse;
import com.smeem.application.port.output.oauth.OauthPort;
import com.smeem.application.port.output.persistence.CorrectionPort;
import com.smeem.application.port.output.persistence.MemberPort;
import com.smeem.common.logger.HookLogger;
import com.smeem.common.logger.LoggingMessage;
Expand All @@ -19,6 +20,7 @@
@RequiredArgsConstructor
@Transactional(readOnly = true)
public class AuthService implements AuthUseCase {
private final CorrectionPort correctionPort;
private final MemberPort memberPort;
private final OauthPort oauthPort;
private final TokenGenerator tokenGenerator;
Expand Down Expand Up @@ -54,6 +56,7 @@ public void signOut(long memberId) {

@Transactional
public void withdraw(long memberId, WithdrawRequest request) {
correctionPort.deleteByMember(memberId);
memberPort.deleteById(memberId);
createWithdrawal(request);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ public record Correction(
String correctedSentence,
@Schema(description = "교정 사유", example = "스펠링 틀림")
@JsonProperty("reason")
String reason
String reason,
@Schema(description = "교정 여부", example = "true")
@JsonProperty("is_corrected")
boolean isCorrected
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,15 @@ public CorrectionsResponse correctDiary(long memberId, long diaryId) {
LocalDate today = LocalDate.now();
String key = getCorrectionCacheKey(memberId, today);

// 제한 횟수 검증
int correctionCount = getOrUpdateCorrectionCount(key, memberId, today);
smeemProperties.getLimit().validateCorrectionLimit(correctionCount);

// 일기 소유권 검증
Diary diary = diaryPort.findById(diaryId);
diary.validateDiaryOwnership(memberId);

// AI 첨삭 및 캐시 업데이트
List<Correction> corrections = createCorrections(diary);
cachePort.incrementInt(key);

Expand All @@ -60,6 +63,6 @@ private int updateCacheWithCorrectionCount(String key, long memberId, LocalDate

private List<Correction> createCorrections(Diary diary) {
List<Correction> corrections = openAiPort.promptCorrections(diary.getContent());
return correctionPort.saveAll(corrections, diary);
return correctionPort.save(corrections, diary);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,13 @@
import java.util.List;

public interface CorrectionPort {
List<Correction> saveAll(List<Correction> corrections, Diary diary);
List<Correction> save(List<Correction> corrections, Diary diary);

int countDistinctByMemberAndDate(long memberId, LocalDate date);

List<Correction> findByDiary(long diary);

void deleteByDiary(long diaryId);

void deleteByMember(long memberId);
}
1 change: 1 addition & 0 deletions smeem-bootstrap/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ dependencies {
implementation project(':smeem-input-http')
implementation project(':smeem-output-persistence')
implementation project(':smeem-output-persistence:postgresql')
implementation project(':smeem-output-persistence:mongodb')
implementation project(':smeem-output-notification')
implementation project(':smeem-output-notification:firebase')
implementation project(':smeem-output-oauth')
Expand Down
1 change: 1 addition & 0 deletions smeem-bootstrap/src/main/resources/application-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ spring:
on-profile: dev
import:
- classpath:postgres-config/application-dev.yml
- classpath:mongo-config/application-dev.yml
- classpath:smeem-config/application-dev.yml
- classpath:notification-config/application-dev.yml
- classpath:common-config/application-dev.yml
Expand Down
1 change: 1 addition & 0 deletions smeem-bootstrap/src/main/resources/application-local.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ spring:
on-profile: local
import:
- classpath:postgres-config/application-local.yml
- classpath:mongo-config/application-local.yml
- classpath:smeem-config/application-local.yml
- classpath:notification-config/application-local.yml
- classpath:common-config/application-local.yml
Expand Down
1 change: 1 addition & 0 deletions smeem-bootstrap/src/main/resources/application-prod.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ spring:
on-profile: prod
import:
- classpath:postgres-config/application-prod.yml
- classpath:mongo-config/application-prod.yml
- classpath:smeem-config/application-prod.yml
- classpath:notification-config/application-prod.yml
- classpath:common-config/application-prod.yml
Expand Down
9 changes: 9 additions & 0 deletions smeem-output-persistence/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
project(':smeem-output-persistence:postgresql') {
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'

// postgresql
implementation 'org.postgresql:postgresql'

// QueryDSL
Expand All @@ -11,6 +13,13 @@ project(':smeem-output-persistence:postgresql') {
}
}

project(':smeem-output-persistence:mongodb') {
dependencies {
// mongodb
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'
}
}

allprojects {
dependencies {
implementation project(':smeem-common')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.smeem.output.persistence.mongodb.adapter;

import com.smeem.application.domain.diary.Correction;
import com.smeem.application.domain.diary.Diary;
import com.smeem.application.port.output.persistence.CorrectionPort;
import com.smeem.output.persistence.mongodb.persistence.document.CorrectionDocument;
import com.smeem.output.persistence.mongodb.persistence.repository.CorrectionRepository;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

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

@Component
@RequiredArgsConstructor
public class CorrectionAdapter implements CorrectionPort {
private final CorrectionRepository correctionRepository;

@Override
public List<Correction> save(List<Correction> corrections, Diary diary) {
CorrectionDocument savedCorrection = correctionRepository.save(new CorrectionDocument(corrections, diary));
return savedCorrection.toDomain();
}

@Override
public int countDistinctByMemberAndDate(long memberId, LocalDate date) {
LocalDateTime startOfDay = date.atStartOfDay();
LocalDateTime endOfDay = date.plusDays(1).atStartOfDay();
return correctionRepository.countByMemberIdAndCreatedAtBetween(memberId, startOfDay, endOfDay);
}

@Override
public List<Correction> findByDiary(long diaryId) {
return correctionRepository.findByDiaryId(diaryId)
.map(CorrectionDocument::toDomain)
.orElseGet(ArrayList::new);
}

@Override
public void deleteByDiary(long diaryId) {
correctionRepository.deleteByDiaryId(diaryId);
}

@Override
public void deleteByMember(long memberId) {
correctionRepository.deleteByMemberId(memberId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.smeem.output.persistence.mongodb.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.config.EnableMongoAuditing;
import org.springframework.data.mongodb.core.convert.DbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultDbRefResolver;
import org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;

@Configuration
@EnableMongoAuditing
@EnableMongoRepositories(basePackages = "com.smeem.output.persistence.mongodb")
public class MongoConfig {

@Bean
public MappingMongoConverter mappingMongoConverter(
MongoDatabaseFactory mongoDatabaseFactory,
MongoMappingContext mongoMappingContext
) {
DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDatabaseFactory);
MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
converter.setTypeMapper(new DefaultMongoTypeMapper(null));
return converter;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.smeem.output.persistence.mongodb.persistence.document;

import com.smeem.application.domain.diary.Correction;
import com.smeem.application.domain.diary.Diary;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

import java.time.LocalDateTime;
import java.util.List;

@Document(collection = "smeem")
@Getter
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class CorrectionDocument {
@Id
private String id;
private List<CorrectionDetail> corrections;
private long memberId;
private long diaryId;
@CreatedDate
private LocalDateTime createdAt;

@NoArgsConstructor(access = AccessLevel.PROTECTED)
@Getter
private static class CorrectionDetail {
private String originContent;
private String correctedContent;
private String reason;
private boolean corrected;

public CorrectionDetail(Correction correction) {
this.originContent = correction.originalSentence();
this.correctedContent = correction.correctedSentence();
this.corrected = correction.isCorrected();
this.reason = corrected ? correction.reason() : null;
}

public Correction toDomain() {
return Correction.builder()
.originalSentence(originContent)
.correctedSentence(correctedContent)
.reason(reason)
.isCorrected(corrected)
.build();
}
}

public CorrectionDocument(List<Correction> corrections, Diary diary) {
this.corrections = corrections.stream().map(CorrectionDetail::new).toList();
this.memberId = diary.getMemberId();
this.diaryId = diary.getId();
}

public List<Correction> toDomain() {
return this.corrections.stream().map(CorrectionDetail::toDomain).toList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.smeem.output.persistence.mongodb.persistence.repository;

import com.smeem.output.persistence.mongodb.persistence.document.CorrectionDocument;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.Optional;

@Repository
public interface CorrectionRepository extends MongoRepository<CorrectionDocument, String> {
int countByMemberIdAndCreatedAtBetween(long memberId, LocalDateTime startAt, LocalDateTime endAt);

Optional<CorrectionDocument> findByDiaryId(long diaryId);

void deleteByDiaryId(long diaryId);

void deleteByMemberId(long memberId);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
config:
activate:
on-profile: dev

data:
mongodb:
uri: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}:27017/smeem?authSource=admin
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
config:
activate:
on-profile: local

data:
mongodb:
uri: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}:27017/smeem?authSource=admin
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
spring:
config:
activate:
on-profile: prod

data:
mongodb:
uri: mongodb://${MONGO_USERNAME}:${MONGO_PASSWORD}@${MONGO_HOST}:27017/smeem?authSource=admin

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
import com.smeem.persistence.postgresql.persistence.entity.MemberEntity;
import com.smeem.persistence.postgresql.persistence.entity.WithdrawEntity;
import com.smeem.persistence.postgresql.persistence.repository.*;
import com.smeem.persistence.postgresql.persistence.repository.diary.CorrectionRepository;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.stereotype.Repository;
Expand All @@ -20,7 +19,6 @@
@Repository
@RequiredArgsConstructor
public class MemberAdapter implements MemberPort {
private final CorrectionRepository correctionRepository;
private final MemberRepository memberRepository;
private final MemberBadgeRepository memberBadgeRepository;
private final DeletedDiaryRepository deletedDiaryRepository;
Expand Down Expand Up @@ -57,7 +55,6 @@ public void deleteById(long id) {
diaryRepository.deleteByMemberId(id);
trainingTimeRepository.deleteByMemberId(id);
memberRepository.deleteById(id);
correctionRepository.deleteByMemberId(id);
}

@Override
Expand Down
Loading

0 comments on commit 778381d

Please sign in to comment.