Skip to content

Commit

Permalink
Merge pull request #215 from skni-kod/issue-214
Browse files Browse the repository at this point in the history
fix: #214 user cannot add more than 1 grade under the material
  • Loading branch information
marcinbator authored Dec 15, 2024
2 parents c31a45d + ab4f6a6 commit 004c85a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,6 @@ public interface GradeRepository extends JpaRepository<Grade, Long> {

@Query("SELECT g FROM Grade g WHERE g.material.id = :materialId AND g.createdDate BETWEEN :from AND :to")
Page<Grade> findGradesByMaterialInDateRange(Long materialId, LocalDateTime from, LocalDateTime to, Pageable pageable);

boolean existsByMaterialIdAndUserId(Long materialId, Long userId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import pl.sknikod.kodemybackend.infrastructure.database.GradeRepository;
import pl.sknikod.kodemybackend.infrastructure.database.Material;
import pl.sknikod.kodemybackend.infrastructure.database.MaterialRepository;
import pl.sknikod.kodemycommons.exception.AlreadyExists409Exception;
import pl.sknikod.kodemycommons.util.PrincipalUtil;
import pl.sknikod.kodemycommons.exception.NotFound404Exception;
import pl.sknikod.kodemycommons.exception.content.ExceptionMsgPattern;
Expand Down Expand Up @@ -93,6 +94,14 @@ public Try<Grade> addGrade(Long materialId, double value) {
ExceptionMsgPattern.ENTITY_NOT_FOUND_BY_PARAM, Material.class.getSimpleName(), "id", materialId
);
})
.andThenTry(() -> {
var userId = principalUtil.getPrincipal().get().getId();
if (gradeRepository.existsByMaterialIdAndUserId(materialId, userId)) {
throw new AlreadyExists409Exception(
ExceptionMsgPattern.ENTITY_ALREADY_EXISTS, "userId"
);
}
})
.mapTry(unused -> new Grade(value, principalUtil.getPrincipal().get().getId(), materialId))
.map(gradeRepository::save)
.onFailure(th -> log.error("Cannot add grade", th));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import pl.sknikod.kodemybackend.infrastructure.database.GradeRepository
import pl.sknikod.kodemybackend.infrastructure.database.MaterialRepository
import pl.sknikod.kodemybackend.infrastructure.store.GradeStore
import pl.sknikod.kodemybackend.infrastructure.store.UserStore
import pl.sknikod.kodemycommons.exception.AlreadyExists409Exception
import pl.sknikod.kodemycommons.exception.NotFound404Exception
import pl.sknikod.kodemycommons.security.UserPrincipal
import pl.sknikod.kodemycommons.util.PrincipalUtil
Expand Down Expand Up @@ -146,6 +147,35 @@ class GradeStoreSpec extends Specification {
Assert.that(grade == result)
}


def "same user cannot add more than 1 grade under the material"() {
given:
materialRepository.existsById(MATERIAL_ID) >> true
principalUtil.getPrincipal() >> Optional.of(new UserPrincipal(
USER_ID, "", false, false,
false, true, new HashSet<SimpleGrantedAuthority>()
))
and:
def savedGrades = []
gradeRepository.save(_ as Grade) >> { Grade g ->
savedGrades << g
return g
}
gradeRepository.existsByMaterialIdAndUserId(_ as Long, _ as Long) >> { Long materialIdArg, Long userIdArg ->
savedGrades.any { it.material.id == materialIdArg && it.userId == userIdArg }
}

when: "add grade to the same material more than 1 time"
def firstAdd = gradeStore.addGrade(1, 1)
def secondAdd = gradeStore.addGrade(1, 2)

then: "second addition is failure"
firstAdd.isSuccess()
secondAdd.isFailure()
secondAdd.failed().get() instanceof AlreadyExists409Exception

}

def "shouldThrowWhenAddGrade"() {
given:
materialRepository.existsById(MATERIAL_ID) >> false
Expand Down

0 comments on commit 004c85a

Please sign in to comment.