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

[WIP][Feat#436] 주간 아티클 TOP5 이메일 전송 배치 추가 #437

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,8 @@ class WebSecurityConfig(
AntPathRequestMatcher("/api/v1/articles/views", HttpMethod.POST.name()),
AntPathRequestMatcher("/api/v1/logs", HttpMethod.POST.name()),
AntPathRequestMatcher("/api/v1/logs/email/articles", HttpMethod.POST.name()),
AntPathRequestMatcher("/batch/**"),
AntPathRequestMatcher("/batch/article", HttpMethod.POST.name()),
AntPathRequestMatcher("/batch/weekly", HttpMethod.GET.name()),

/** 인증 불필요 */
AntPathRequestMatcher("/api/v1/members", HttpMethod.POST.name()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
package com.few.api.web.controller.batch

import com.few.batch.service.article.BatchSendArticleEmailService
import com.few.batch.service.article.BatchSendWeeklyArticleEmailService
import org.springframework.beans.factory.annotation.Value
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.bind.annotation.*

@Validated
@RestController
@RequestMapping("/batch")
class BatchController(
private val batchSendArticleEmailService: BatchSendArticleEmailService,
private val batchSendWeeklyArticleEmailService: BatchSendWeeklyArticleEmailService,
@Value("\${auth.batch}") private val auth: String,
) {

Expand All @@ -23,4 +22,12 @@ class BatchController(
}
batchSendArticleEmailService.execute()
}

@GetMapping("/weekly")
fun batchWeeklyArticle(@RequestParam(value = "auth") auth: String) {
if (this.auth != auth) {
throw IllegalAccessException("Invalid Permission")
}
batchSendWeeklyArticleEmailService.execute()
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.few.batch.service.article

import com.few.batch.service.article.reader.WeeklyArticleIdReader
import io.github.oshai.kotlinlogging.KotlinLogging
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class BatchSendWeeklyArticleEmailService(
private val weeklyArticleIdReader: WeeklyArticleIdReader,
) {
private val log = KotlinLogging.logger {}

@Transactional
fun execute() {
val articleIds = weeklyArticleIdReader.browseWeeklyArticleIds()

log.debug { "Read Weekly TOP5 Article IDs: $articleIds " }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.few.batch.service.article.dto

data class ArticleIdAndVewCount(
val articleId: Long,
val viewCount: Int,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.few.batch.service.article.reader

import com.few.batch.service.article.dto.ArticleIdAndVewCount
import jooq.jooq_dsl.tables.ArticleViewHis.ARTICLE_VIEW_HIS
import org.jooq.DSLContext
import org.jooq.impl.DSL
import org.springframework.stereotype.Component
import java.time.LocalDate
import java.time.temporal.TemporalAdjusters

@Component
class WeeklyArticleIdReader(
private val dslContext: DSLContext,
) {

fun browseWeeklyArticleIds(): List<ArticleIdAndVewCount> {
val today = LocalDate.now()
val thisSunday = today.with(TemporalAdjusters.nextOrSame(java.time.DayOfWeek.SUNDAY))
val lastSunday = today.with(TemporalAdjusters.previousOrSame(java.time.DayOfWeek.SUNDAY)).minusWeeks(1)

return selectArticleIdAndViewCountLimit5(thisSunday, lastSunday)
}

fun selectArticleIdAndViewCountLimit5(fromDay: LocalDate, toDay: LocalDate): List<ArticleIdAndVewCount> {
val viewCount = DSL.count()

return dslContext.select(
ARTICLE_VIEW_HIS.ARTICLE_MST_ID.`as`(ArticleIdAndVewCount::articleId.name),
DSL.count().`as`(ArticleIdAndVewCount::viewCount.name)
)
.from(ARTICLE_VIEW_HIS)
.where(ARTICLE_VIEW_HIS.CREATED_AT.between(fromDay.atStartOfDay(), toDay.atTime(23, 59, 59)))
.groupBy(ARTICLE_VIEW_HIS.ARTICLE_MST_ID)
.orderBy(viewCount.desc(), ARTICLE_VIEW_HIS.ARTICLE_MST_ID.desc())
.limit(5)
.fetchInto(ArticleIdAndVewCount::class.java)
}
}
Loading