Skip to content

Commit

Permalink
Ready for step 1 RELEASE (#29)
Browse files Browse the repository at this point in the history
* More frequent API poke

* Source problem count

* Ready for step 1 RELEASE

---------

Co-authored-by: Naglis <naglis.suliokas@gmail.com>
  • Loading branch information
naglissul and Naglis authored Aug 23, 2024
1 parent 07d0a52 commit 296d863
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cron-job-to-keep-api-alive.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: HTTP Request every 14 minutes

on:
schedule:
- cron: "*/14 * * * *"
- cron: "*/13 * * * *"

jobs:
send_requests:
Expand Down
25 changes: 15 additions & 10 deletions src/main/kotlin/lt/skafis/bankas/controller/ProblemController.kt
Original file line number Diff line number Diff line change
@@ -1,37 +1,35 @@
package lt.skafis.bankas.controller

import com.google.api.client.http.HttpResponse
import io.netty.handler.codec.http.HttpResponseStatus
import io.swagger.v3.oas.annotations.security.SecurityRequirement
import io.swagger.v3.oas.annotations.tags.Tag
import lt.skafis.bankas.config.Logged
import lt.skafis.bankas.config.RequiresRoleAtLeast
import lt.skafis.bankas.dto.ProblemPostDto
import lt.skafis.bankas.model.Problem
import lt.skafis.bankas.model.ProblemMeta
import lt.skafis.bankas.model.Role
import lt.skafis.bankas.service.ProblemMetaService
import lt.skafis.bankas.service.ProblemService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
// @RestController
@RequestMapping("/problem")
@Tag(name = "Problem Controller", description = "SUPER_ADMIN")
@SecurityRequirement(name = "bearerAuth")
@RequiresRoleAtLeast(Role.SUPER_ADMIN)
@Logged
class ProblemController {

@Autowired
private lateinit var problemService: ProblemService

@Autowired
private lateinit var problemMetaService: ProblemMetaService

@PostMapping
fun createProblem(@RequestBody problemPostDto: ProblemPostDto): ResponseEntity<Problem> {
fun createProblem(
@RequestBody problemPostDto: ProblemPostDto,
): ResponseEntity<Problem> {
val problem = problemService.createProblem(problemPostDto)
return ResponseEntity.ok(problem)
}
Expand All @@ -43,19 +41,26 @@ class ProblemController {
}

@GetMapping("/{id}")
fun getProblemById(@PathVariable id: String): ResponseEntity<Problem> {
fun getProblemById(
@PathVariable id: String,
): ResponseEntity<Problem> {
val problem = problemService.getProblemById(id)
return ResponseEntity.ok(problem)
}

@PutMapping("/{id}")
fun updateProblem(@PathVariable id: String, @RequestBody problemPostDto: ProblemPostDto): ResponseEntity<Problem> {
fun updateProblem(
@PathVariable id: String,
@RequestBody problemPostDto: ProblemPostDto,
): ResponseEntity<Problem> {
val updatedProblem = problemService.updateProblem(id, problemPostDto)
return ResponseEntity.ok(updatedProblem)
}

@DeleteMapping("/{id}")
fun deleteProblem(@PathVariable id: String): ResponseEntity<Void> {
fun deleteProblem(
@PathVariable id: String,
): ResponseEntity<Void> {
problemService.deleteProblem(id)
return ResponseEntity.ok().build()
}
Expand All @@ -65,4 +70,4 @@ class ProblemController {
problemMetaService.clearUsedSkfCodeList()
return ResponseEntity.ok().build()
}
}
}
24 changes: 16 additions & 8 deletions src/main/kotlin/lt/skafis/bankas/controller/SourceController.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,27 @@ import io.swagger.v3.oas.annotations.tags.Tag
import lt.skafis.bankas.config.Logged
import lt.skafis.bankas.config.RequiresRoleAtLeast
import lt.skafis.bankas.dto.SourcePostDto
import lt.skafis.bankas.model.Source
import lt.skafis.bankas.model.Role
import lt.skafis.bankas.model.Source
import lt.skafis.bankas.service.SourceService
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
// @RestController
@RequestMapping("/source")
@Tag(name = "Source Controller", description = "SUPER_ADMIN")
@SecurityRequirement(name = "bearerAuth")
@RequiresRoleAtLeast(Role.SUPER_ADMIN)
@Logged
class SourceController {

@Autowired
private lateinit var sourceService: SourceService

@PostMapping
fun createSource(@RequestBody sourcePostDto: SourcePostDto): ResponseEntity<Source> {
fun createSource(
@RequestBody sourcePostDto: SourcePostDto,
): ResponseEntity<Source> {
println(sourcePostDto)
val source = sourceService.createSource(sourcePostDto)
return ResponseEntity.ok(source)
Expand All @@ -37,20 +38,27 @@ class SourceController {
}

@GetMapping("/{id}")
fun getSourceById(@PathVariable id: String): ResponseEntity<Source> {
fun getSourceById(
@PathVariable id: String,
): ResponseEntity<Source> {
val source = sourceService.getSourceById(id)
return ResponseEntity.ok(source)
}

@PutMapping("/{id}")
fun updateSource(@PathVariable id: String, @RequestBody sourcePostDto: SourcePostDto): ResponseEntity<Source> {
fun updateSource(
@PathVariable id: String,
@RequestBody sourcePostDto: SourcePostDto,
): ResponseEntity<Source> {
val updatedSource = sourceService.updateSource(id, sourcePostDto)
return ResponseEntity.ok(updatedSource)
}

@DeleteMapping("/{id}")
fun deleteSource(@PathVariable id: String): ResponseEntity<Void> {
fun deleteSource(
@PathVariable id: String,
): ResponseEntity<Void> {
sourceService.deleteSource(id)
return ResponseEntity.ok().build()
}
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/lt/skafis/bankas/dto/SourceDisplayDto.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ data class SourceDisplayDto(
val reviewStatus: ReviewStatus = ReviewStatus.PENDING,
val reviewHistory: String = "",
val authorUsername: String = "",
val problemCount: Int = 0,
val createdOn: String = Instant.now().toString(),
val lastModifiedOn: String = Instant.now().toString()
)
val lastModifiedOn: String = Instant.now().toString(),
)
15 changes: 8 additions & 7 deletions src/main/kotlin/lt/skafis/bankas/model/Source.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,21 @@ data class Source(
val reviewHistory: String = "",
val authorId: String = "",
val createdOn: String = Instant.now().toString(),
val lastModifiedOn: String = Instant.now().toString()
): Identifiable {
val lastModifiedOn: String = Instant.now().toString(),
) : Identifiable {
fun toDisplayDto(
authorUsername: String = "",
): SourceDisplayDto {
return SourceDisplayDto(
problemCount: Int = 0,
): SourceDisplayDto =
SourceDisplayDto(
id = this.id,
name = this.name,
description = this.description,
reviewStatus = this.reviewStatus,
reviewHistory = this.reviewHistory,
authorUsername = authorUsername,
problemCount = problemCount,
createdOn = this.createdOn,
lastModifiedOn = this.lastModifiedOn
lastModifiedOn = this.lastModifiedOn,
)
}
}
}
10 changes: 10 additions & 0 deletions src/main/kotlin/lt/skafis/bankas/repository/ProblemRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,14 @@ class ProblemRepository(

return pagedDocuments.mapNotNull { it.toObject(Problem::class.java) }
}

fun countBySource(sourceId: String): Long =
firestore
.collection(collectionPath)
.whereEqualTo("sourceId", sourceId)
.get()
.get()
.documents
.size
.toLong()
}
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ class ApprovalServiceImpl : ApprovalService {
if (source.reviewStatus == ReviewStatus.APPROVED) {
val sourceProblems = problemRepository.getBySourceId(sourceId)
sourceProblems.forEach {
problemRepository.update(it.copy(skfCode = "", isApproved = false), it.id)
problemRepository.update(it.copy(isApproved = false), it.id)
if (it.isApproved) {
metaService.removeSkfCodeFromUsedList(it.skfCode)
}
Expand All @@ -124,7 +124,8 @@ class ApprovalServiceImpl : ApprovalService {
size,
(page * size).toLong(),
).map {
it.toDisplayDto(userService.getUsernameById(it.authorId))
val count = problemRepository.countBySource(it.id)
it.toDisplayDto(userService.getUsernameById(it.authorId), count.toInt())
}
}

Expand Down Expand Up @@ -249,7 +250,9 @@ class ApprovalServiceImpl : ApprovalService {
throw IllegalAccessException("User $userId does not own source ${problem.sourceId}")
}
problemRepository.delete(problemId)
metaService.removeSkfCodeFromUsedList(problem.skfCode)
if (problem.skfCode.isNotEmpty()) {
metaService.removeSkfCodeFromUsedList(problem.skfCode)
}
if (problem.problemImagePath.startsWith("problems/")) {
storageRepository.deleteImage(problem.problemImagePath)
}
Expand Down Expand Up @@ -290,7 +293,8 @@ class ApprovalServiceImpl : ApprovalService {
size,
(page * size).toLong(),
).map {
it.toDisplayDto(userService.getUsernameById(it.authorId))
val count = problemRepository.countBySource(it.id)
it.toDisplayDto(userService.getUsernameById(it.authorId), count.toInt())
}

override fun updateProblemTexts(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,8 @@ class PublicServiceImpl : PublicService {
size,
(page * size).toLong(),
).map {
val count = problemRepository.countBySource(it.id)
val authorUsername = userService.getUsernameById(it.authorId)
it.toDisplayDto(authorUsername)
it.toDisplayDto(authorUsername, count.toInt())
}
}

0 comments on commit 296d863

Please sign in to comment.