Skip to content
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
20 changes: 19 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
FROM gradle:7.4.2-jdk17 as builder

WORKDIR /app

COPY gradlew /app/
COPY gradle /app/gradle
COPY build.gradle.kts /app/
COPY settings.gradle.kts /app/
COPY src /app/src

RUN chmod +x gradlew

RUN ./gradlew build

FROM openjdk:17

WORKDIR /app
EXPOSE 8080

COPY ./build/libs/ItmochanBackend.jar .

CMD ["java","-jar", "ItmochanBackend.jar"]

EXPOSE 8080
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,12 @@ dependencies {
implementation("org.springframework.boot:spring-boot-starter-web")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.postgresql:postgresql")
implementation("org.postgresql:postgresql:42.7.3")
testImplementation("org.springframework.boot:spring-boot-starter-test")
implementation("io.minio:minio:8.5.17")
implementation("org.flywaydb:flyway-core:10.13.0")
runtimeOnly("org.flywaydb:flyway-database-postgresql:10.13.0")

//spring security
implementation("io.jsonwebtoken:jjwt-api:0.12.3")
implementation("io.jsonwebtoken:jjwt-impl:0.12.3")
Expand Down
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ services:
- itmochan-network
postgres:
container_name: postgres-container-itmochan
image: postgres
image: postgres:latest
environment:
POSTGRES_USER: postgres
POSTGRES_PASSWORD: password
Expand Down
Empty file modified gradlew
100644 → 100755
Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ interface PollAnswerRepository : CrudRepository<PollAnswer, Long> {

@Modifying
@Transactional
@Query("CALL vote_in_poll(:user_id, :poll_id, array[:answers])")
fun voteInPoll(
@Param("user_id") userId: Long,
@Param("poll_id") pollId: Long,
@Param("answers") answersIds: List<Long>,
)
@Query("UPDATE PollAnswer pa SET pa.votesNumber = pa.votesNumber + 1 WHERE pa.pollAnswerId IN :answers AND pa.pollId = :pollId")
fun incrementVotes(@Param("pollId") pollId: Long, @Param("answers") answers: List<Long>)

@Modifying
@Transactional
@Query("INSERT INTO VotedUsers (poll_id, user_id) VALUES (:pollId, :userId)")
fun insertVotedUser(@Param("pollId") pollId: Long, @Param("userId") userId: Long)

fun findPollAnswersByPollId(pollId: Long) : List<PollAnswer>
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@ import org.springframework.data.jdbc.repository.query.Modifying
import org.springframework.data.jdbc.repository.query.Query
import org.springframework.data.repository.CrudRepository
import org.springframework.data.repository.query.Param
import java.util.*
import org.springframework.transaction.annotation.Transactional
import java.util.Optional

interface TrashRepository : CrudRepository<Trash, Long> {
@Modifying
@Query("CALL throw_in_trash(:comment_id, :reason)")
fun throwInTrash(
@Param("comment_id") commentId : Long,
@Param("reason") reason : String?,
)
@Transactional
@Query("INSERT INTO Trash (comment_id, reason) VALUES (:commentId, :reason)")
fun insertIntoTrash(@Param("commentId") commentId: Long, @Param("reason") reason: String?)

@Modifying
@Transactional
@Query("UPDATE Comments SET trashed = true WHERE comment_id = :commentId")
fun updateCommentAsTrashed(@Param("commentId") commentId: Long)

fun findTrashByCommentId(commentId: Long) : Optional<Trash>
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package io.github.secsdev.itmochan.service.impl

import io.github.secsdev.itmochan.entity.PollDTO
import io.github.secsdev.itmochan.exception.EmptyAnswersListException
import io.github.secsdev.itmochan.exception.NoSuchPollException
import io.github.secsdev.itmochan.exception.UserAlreadyVotedException
import io.github.secsdev.itmochan.repository.PollAnswerRepository
import io.github.secsdev.itmochan.repository.PollRepository
import io.github.secsdev.itmochan.repository.VotedUsersRepository
import io.github.secsdev.itmochan.response.PollResponse
import io.github.secsdev.itmochan.exception.EmptyAnswersListException
import io.github.secsdev.itmochan.exception.NoSuchPollException
import io.github.secsdev.itmochan.exception.UserAlreadyVotedException
import io.github.secsdev.itmochan.service.PollService
import io.github.secsdev.itmochan.service.UserService
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class PollServiceImpl (
Expand Down Expand Up @@ -45,8 +46,7 @@ class PollServiceImpl (
val votedUser = votedUsersRepository.findVotedUsersByUserIdAndPollId(user.userId, pollId)
if (votedUser.isPresent)
throw UserAlreadyVotedException("You have already voted in this poll")

pollAnswerRepository.voteInPoll(user.userId, pollId, answersIds)
performVoteOperation(pollId = pollId, answersIds = answersIds, userId = user.userId)
}

override fun getPoll(pollId: Long): PollResponse {
Expand All @@ -65,4 +65,14 @@ class PollServiceImpl (
throw NoSuchPollException("No such poll was found")
return poll.get().pollId
}

@Transactional
private fun performVoteOperation(
userId: Long,
pollId: Long,
answersIds: List<Long>,
) {
pollAnswerRepository.incrementVotes(pollId = pollId, answers = answersIds)
pollAnswerRepository.insertVotedUser(pollId = pollId, userId = userId)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ package io.github.secsdev.itmochan.service.impl

import io.github.secsdev.itmochan.entity.Trash
import io.github.secsdev.itmochan.entity.TrashDTO
import io.github.secsdev.itmochan.repository.TrashRepository
import io.github.secsdev.itmochan.exception.AlreadyTrashedException
import io.github.secsdev.itmochan.exception.NoSuchTrashException
import io.github.secsdev.itmochan.repository.TrashRepository
import io.github.secsdev.itmochan.service.CommentService
import io.github.secsdev.itmochan.service.TrashService
import org.springframework.stereotype.Service
import org.springframework.transaction.annotation.Transactional

@Service
class TrashServiceImpl(
Expand All @@ -18,7 +19,7 @@ class TrashServiceImpl(
val trashO = trashRepository.findTrashByCommentId(trash.commentId)
if (trashO.isPresent)
throw AlreadyTrashedException("This comment has been already trashed")
trashRepository.throwInTrash(trash.commentId, trash.reason)
performTrashOperation(trash = trash)
}

override fun getTrash(commentId : Long) : Trash {
Expand All @@ -28,4 +29,10 @@ class TrashServiceImpl(
throw NoSuchTrashException("No such trash was found")
return trash.get()
}

@Transactional
private fun performTrashOperation(trash: TrashDTO) {
trashRepository.insertIntoTrash(commentId = trash.commentId, reason = trash.reason)
trashRepository.updateCommentAsTrashed(commentId = trash.commentId)
}
}
12 changes: 6 additions & 6 deletions src/main/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ spring:
allow-bean-definition-overriding: true
datasource:
driver-class-name: org.postgresql.Driver
url: jdbc:postgresql://postgres:5432/
url: jdbc:postgresql://postgres:5432/postgres
username: postgres
password: password
jpa:
generate-ddl: true
show-sql: true
sql:
init:
schema-locations: classpath:sql/schema.sql
data-locations: classpath:sql/data.sql
mode: always
flyway:
enabled: true
locations: classpath:db/migrations
baseline-on-migrate: true
validate-on-migrate: true
servlet.multipart:
max-file-size: 10MB
max-request-size: 10MB
Expand Down
Loading