Skip to content

Commit

Permalink
Merge pull request #205 from dnd-side-project/dev
Browse files Browse the repository at this point in the history
release v0.3.0
  • Loading branch information
eun-seong authored Sep 30, 2024
2 parents b2d5c6f + ea0ee21 commit 7661721
Show file tree
Hide file tree
Showing 69 changed files with 1,282 additions and 594 deletions.
8 changes: 5 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,9 @@ ext {
set('jwtVersion', '0.12.5')
set('springdocOpenapiUIVersion', '2.3.0')
set('mockitoVersion', '5.10.0')
}

ext {
set('jwtVersion', '0.12.5')
set('springdocOpenapiUIVersion', '2.3.0')
set('springRetryVersion', '2.0.7')
}

group = 'com.dnd'
Expand All @@ -33,6 +31,10 @@ dependencies {
implementation 'org.springframework.boot:spring-boot-starter-validation'
implementation 'org.springframework.boot:spring-boot-starter-data-mongodb'

/* spring retry */
implementation("org.springframework.retry:spring-retry:${springRetryVersion}")
implementation("org.springframework:spring-aspects")

/* micrometer */
implementation 'io.micrometer:micrometer-registry-prometheus'

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ public enum ApplicationErrorType {
NOT_FOUND_USER(HttpStatus.NOT_FOUND, "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์œ ์ €์ž…๋‹ˆ๋‹ค."),
EXISTING_USER(HttpStatus.CONFLICT, "์ด๋ฏธ ์กด์žฌํ•˜๋Š” ์œ ์ €์ž…๋‹ˆ๋‹ค."),

/**
* Wiki Error Type
*/
NOT_FOUND_WIKI(HttpStatus.NOT_FOUND, "์กด์žฌํ•˜์ง€ ์•Š๋Š” ์œ„ํ‚ค์ž…๋‹ˆ๋‹ค."),
INVALID_WIKI_TYPE(HttpStatus.CONFLICT, "์ž˜๋ชป๋œ ์œ„ํ‚ค ํƒ€์ž…์ž…๋‹ˆ๋‹ค."),

/**
* Question Error Type
*/
Expand All @@ -55,6 +61,7 @@ public enum ApplicationErrorType {
NOT_STRING_ANSWER(HttpStatus.NOT_FOUND, "๋ฌธ์žํ˜• ๋‹ต๋ณ€์ด ์•„๋‹™๋‹ˆ๋‹ค."),
CANNOT_SEND_SURVEY_TO_MYSELF(HttpStatus.BAD_REQUEST, "์ž์‹ ์—๊ฒŒ ์„ค๋ฌธ์„ ๋ณด๋‚ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
ANSWER_REASON_REQUIRED(HttpStatus.CONFLICT, "reason ํ•„๋“œ๊ฐ€ ํ•„์š”ํ•œ ์งˆ๋ฌธ์ž…๋‹ˆ๋‹ค."),
INVALID_ANSWER_TYPE(HttpStatus.BAD_REQUEST, "๋‹ต๋ณ€ ํƒ€์ž…์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค."),

/**
* Survey Error Type
Expand All @@ -64,10 +71,17 @@ public enum ApplicationErrorType {
INVALID_BORROWING_LIMIT(HttpStatus.BAD_REQUEST, "๋นŒ๋ฆด ์ˆ˜ ์žˆ๋Š” ๋ˆ์€ 0 ์ด์ƒ 10์–ต ์ดํ•˜์ž…๋‹ˆ๋‹ค."),
INVALID_SURVEY_OWNER(HttpStatus.CONFLICT, "์„ค๋ฌธ ์†Œ์œ ์ž๊ฐ€ ์•„๋‹™๋‹ˆ๋‹ค."),

/**
* Dashboard Error Type
*/
INVALID_DASHBOARD_TYPE(HttpStatus.CONFLICT, "์ž˜๋ชป๋œ ๋Œ€์‹œ๋ณด๋“œ ํƒ€์ž…์ž…๋‹ˆ๋‹ค."),

/**
* Filter Error Type
*/
INVALID_FILTER(HttpStatus.BAD_REQUEST, "๋™์‹œ์— ๋‘๊ฐ€์ง€ ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
INVALID_FILTER(HttpStatus.BAD_REQUEST, "๋™์‹œ์— ๋‘๊ฐ€์ง€ ํ•„ํ„ฐ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค."),
INVALID_QUESTION_WIKI_TYPE(HttpStatus.BAD_REQUEST, "์งˆ๋ฌธ์˜ ์œ„ํ‚ค ํƒ€์ž…์ด ๋‹ค๋ฆ…๋‹ˆ๋‹ค."),
;


@Getter
Expand Down
19 changes: 19 additions & 0 deletions src/main/java/com/dnd/namuiwiki/common/util/ListUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.dnd.namuiwiki.common.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class ListUtils {
public static <T> List<T> convertList(Object obj) {
if (obj.getClass().isArray()) {
return Arrays.asList((T[]) obj);
} else if (obj instanceof Collection) {
return new ArrayList<>((Collection<T>) obj);
} else {
throw new ClassCastException("Object is not a List.");
}

}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.dnd.namuiwiki.config;

import com.dnd.namuiwiki.domain.dashboard.DashboardService;
import com.dnd.namuiwiki.domain.statistic.StatisticsService;
import com.dnd.namuiwiki.domain.survey.SurveyEventHandler;
import lombok.RequiredArgsConstructor;
Expand All @@ -10,10 +11,11 @@
@RequiredArgsConstructor
public class EventConfiguration {
private final StatisticsService statisticsService;
private final DashboardService dashboardService;

@Bean
public SurveyEventHandler surveyEventHandler() {
return new SurveyEventHandler(statisticsService);
return new SurveyEventHandler(statisticsService, dashboardService);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.dnd.namuiwiki.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;

@Configuration
@EnableRetry(proxyTargetClass=true)
public class RetryConfiguration {
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.dnd.namuiwiki.domain.jwt.dto.TokenUserInfoDto;
import com.dnd.namuiwiki.domain.survey.type.Period;
import com.dnd.namuiwiki.domain.survey.type.Relation;
import com.dnd.namuiwiki.domain.wiki.WikiType;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
Expand All @@ -32,9 +33,10 @@ public class DashboardController {
public ResponseEntity<?> getDashboard(
@JwtAuthorization TokenUserInfoDto tokenUserInfoDto,
@RequestParam(required = false) Period period,
@RequestParam(required = false) Relation relation
@RequestParam(required = false) Relation relation,
@RequestParam WikiType wikiType
) {
DashboardDto response = dashboardService.getDashboard(tokenUserInfoDto, period, relation);
DashboardDto response = dashboardService.getDashboard(tokenUserInfoDto, wikiType, period, relation);
return ResponseDto.ok(response);
}

Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.dnd.namuiwiki.domain.dashboard;


import com.dnd.namuiwiki.domain.dashboard.model.entity.Dashboard;
import com.dnd.namuiwiki.domain.statistic.model.Statistics;
import com.dnd.namuiwiki.domain.survey.model.entity.Answer;
import com.dnd.namuiwiki.domain.survey.type.Period;
import com.dnd.namuiwiki.domain.survey.type.Relation;
import com.dnd.namuiwiki.domain.user.entity.User;
import com.dnd.namuiwiki.domain.wiki.WikiType;
import com.dnd.namuiwiki.external.DiscordClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.OptimisticLockingFailureException;
import org.springframework.retry.annotation.Backoff;
import org.springframework.retry.annotation.Recover;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@RequiredArgsConstructor
@Service
public class DashboardInternalProxyService {
private final DashboardRepository dashboardRepository;
private final DiscordClient discordClient;

@Retryable(
retryFor = {OptimisticLockingFailureException.class},
maxAttempts = 3,
backoff = @Backoff(delay = 30 * 1000L)
)
public void updateDashboard(User owner, WikiType wikiType, Period period, Relation relation, List<Answer> answers) {
insertDashboardIfNotExist(owner, wikiType, period, relation, answers);
Dashboard dashboard = dashboardRepository.findByUserAndWikiTypeAndPeriodAndRelation(owner, wikiType, period, relation)
.orElseGet(() -> {
Dashboard d = Dashboard.builder()
.user(owner)
.wikiType(wikiType)
.period(period)
.relation(relation)
.statistics(Statistics.from(answers.stream().map(Answer::getQuestion).toList()))
.build();
return dashboardRepository.save(d);
});

dashboard.updateStatistics(answers);
dashboardRepository.save(dashboard);
}

@Recover
private void recoverForUpdateDashboard(Exception e) {
log.error("Failed to update dashboard", e);
discordClient.sendMessage(e.toString());
}

private void insertDashboardIfNotExist(User owner, WikiType wikiType, Period period, Relation relation, List<Answer> answers) {
if (!dashboardRepository.existsByUserAndWikiTypeAndPeriodAndRelation(owner, wikiType, period, relation)) {
dashboardRepository.save(Dashboard.builder()
.user(owner)
.wikiType(wikiType)
.period(period)
.relation(relation)
.statistics(Statistics.from(answers.stream().map(Answer::getQuestion).toList()))
.build());
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
import com.dnd.namuiwiki.domain.survey.type.Period;
import com.dnd.namuiwiki.domain.survey.type.Relation;
import com.dnd.namuiwiki.domain.user.entity.User;
import com.dnd.namuiwiki.domain.wiki.WikiType;
import org.springframework.data.mongodb.repository.MongoRepository;

import java.util.Optional;

public interface DashboardRepository extends MongoRepository<Dashboard, String>, DashboardCustomRepository {
Optional<Dashboard> findByUserAndPeriodAndRelation(User user, Period period, Relation relation);
boolean existsByUserAndPeriodAndRelation(User user, Period period, Relation relation);
public interface DashboardRepository extends MongoRepository<Dashboard, String> {
Optional<Dashboard> findByUserAndWikiTypeAndPeriodAndRelation(User user, WikiType wikiType, Period period, Relation relation);

boolean existsByUserAndWikiTypeAndPeriodAndRelation(User user, WikiType wikiType, Period period, Relation relation);
}
Loading

0 comments on commit 7661721

Please sign in to comment.