diff --git a/src/main/java/br/com/darksun/controller/VoteController.java b/src/main/java/br/com/darksun/controller/VoteController.java index f73c7f3..0b60ae9 100644 --- a/src/main/java/br/com/darksun/controller/VoteController.java +++ b/src/main/java/br/com/darksun/controller/VoteController.java @@ -46,6 +46,35 @@ public Response readById( @PathParam( "id" ) Long id ) { return Response.ok( service.readById( id ) ).build( ); } + @GET + @Path( "mine" ) + @RolesAllowed( { HOST_ROLE, GUEST_ROLE } ) + @Produces( MediaType.APPLICATION_JSON ) + public Response readMyVotes( @Context SecurityContext securityContext ) { + return Response.ok( service.readMyVotes( securityContext.getUserPrincipal( ).getName( ) ) ) + .build( ); + } + + @GET + @Path( "results" ) + @RolesAllowed( { HOST_ROLE, GUEST_ROLE } ) + @Produces( MediaType.APPLICATION_JSON ) + public Response results( ) { + return Response.ok( service.results( ) ).build( ); + } + + @PATCH + @Path( "{id}/{score}" ) + @Transactional + @RolesAllowed( { HOST_ROLE, GUEST_ROLE } ) + @Produces( MediaType.APPLICATION_JSON ) + public Response changeScore( @PathParam( "id" ) Long id, @PathParam( "score" ) Short score, + @Context SecurityContext securityContext ) { + return Response.ok( + service.changeScore( id, score, securityContext.getUserPrincipal( ).getName( ) ) ) + .build( ); + } + @DELETE @Path( "{id}" ) @Transactional @@ -54,4 +83,14 @@ public Response delete( @PathParam( "id" ) Long id ) { service.delete( id ); return Response.noContent( ).build( ); } + + @DELETE + @Path( "mine/{id}" ) + @Transactional + @RolesAllowed( { HOST_ROLE, GUEST_ROLE } ) + public Response deleteMyVotes( @PathParam( "id" ) Long id, + @Context SecurityContext securityContext ) { + service.deleteMyVote( id, securityContext.getUserPrincipal( ).getName( ) ); + return Response.noContent( ).build( ); + } } diff --git a/src/main/java/br/com/darksun/repository/VoteRepository.java b/src/main/java/br/com/darksun/repository/VoteRepository.java index 3c42c2d..e918493 100644 --- a/src/main/java/br/com/darksun/repository/VoteRepository.java +++ b/src/main/java/br/com/darksun/repository/VoteRepository.java @@ -6,6 +6,7 @@ import io.quarkus.hibernate.orm.panache.PanacheRepository; import jakarta.enterprise.context.ApplicationScoped; +import java.util.List; import java.util.Optional; @ApplicationScoped @@ -14,4 +15,8 @@ public Optional< Vote > findByItsForAndWhoVotesOptional( Song itsFor, Guest whoV return this.find( "itsFor = ?1 AND whoVotes = ?2", itsFor, whoVotes ) .firstResultOptional( ); } + + public List< Vote > findAllByWhoVotes( Guest whoVotes ) { + return this.find( "whoVotes", whoVotes ).list( ); + } } diff --git a/src/main/java/br/com/darksun/service/VoteService.java b/src/main/java/br/com/darksun/service/VoteService.java index 96a6e5f..7f9c400 100644 --- a/src/main/java/br/com/darksun/service/VoteService.java +++ b/src/main/java/br/com/darksun/service/VoteService.java @@ -1,5 +1,6 @@ package br.com.darksun.service; +import br.com.darksun.model.Guest; import br.com.darksun.model.Vote; import br.com.darksun.repository.VoteRepository; import br.com.darksun.util.Utils; @@ -38,6 +39,11 @@ public Vote readById( Long id ) { "Vote not found with ID: " + id ) ); } + public List< Vote > readMyVotes( String whoVotesName ) { + Guest whoVotes = guestService.readByName( whoVotesName ); + return repository.findAllByWhoVotes( whoVotes ); + } + public List< String > results( ) { List< String > results = new ArrayList<>( ); Map< String, Integer > musicAndItVotes = new HashMap<>( ); @@ -63,6 +69,14 @@ public List< String > results( ) { return results; } + public Vote changeScore( Long id, Short score, String whoVotesName ) { + Vote vote = readById( id ); + vote.setScore( score ); + applyBusinessRules( vote, whoVotesName ); + repository.getEntityManager( ).merge( vote ); + return vote; + } + public void delete( Long id ) { boolean wasDeleted = repository.deleteById( id ); if ( !wasDeleted ) { @@ -70,6 +84,16 @@ public void delete( Long id ) { } } + public void deleteMyVote( Long id, String whoVotesName ) { + Vote vote = readById( id ); + if ( vote.getWhoVotes( ) == null || !vote.getWhoVotes( ) + .getName( ) + .equals( whoVotesName ) ) { + throw new IllegalArgumentException( "This vote is not yours" ); + } + delete( id ); + } + private void applyBusinessRules( Vote vote, String whoVotesName ) { vote.setWhoVotes( guestService.readByName( whoVotesName ) ); vote.setItsFor( songService.readById( vote.getItsFor( ).getId( ) ) ); diff --git a/tools/HttpRequests/Vote/ChangeScore.bru b/tools/HttpRequests/Vote/ChangeScore.bru new file mode 100644 index 0000000..7d06500 --- /dev/null +++ b/tools/HttpRequests/Vote/ChangeScore.bru @@ -0,0 +1,29 @@ +meta { + name: ChangeScore + type: http + seq: 6 +} + +patch { + url: {{java-url}}/votes/2/2 + body: none + auth: basic +} + +auth:basic { + username: {{username}} + password: {{password}} +} + +body:json { + { + "id": 1, + "score": 3, + "itsFor": { + "id": 1 + }, + "whoVotes": { + "id": 1 + } + } +} diff --git a/tools/HttpRequests/Vote/Create.bru b/tools/HttpRequests/Vote/Create.bru index 332fdbe..3186a65 100644 --- a/tools/HttpRequests/Vote/Create.bru +++ b/tools/HttpRequests/Vote/Create.bru @@ -1,7 +1,7 @@ meta { name: Create type: http - seq: 4 + seq: 5 } post { diff --git a/tools/HttpRequests/Vote/Delete.bru b/tools/HttpRequests/Vote/Delete.bru index 31ffba2..7861acd 100644 --- a/tools/HttpRequests/Vote/Delete.bru +++ b/tools/HttpRequests/Vote/Delete.bru @@ -1,7 +1,7 @@ meta { name: Delete type: http - seq: 5 + seq: 7 } delete { diff --git a/tools/HttpRequests/Vote/DeleteMyVote.bru b/tools/HttpRequests/Vote/DeleteMyVote.bru new file mode 100644 index 0000000..ade2fc9 --- /dev/null +++ b/tools/HttpRequests/Vote/DeleteMyVote.bru @@ -0,0 +1,29 @@ +meta { + name: DeleteMyVote + type: http + seq: 8 +} + +delete { + url: {{java-url}}/votes/mine/2 + body: none + auth: basic +} + +auth:basic { + username: {{username}} + password: {{password}} +} + +body:json { + { + "id": 1, + "score": 3, + "itsFor": { + "id": 1 + }, + "whoVotes": { + "id": 1 + } + } +} diff --git a/tools/HttpRequests/Vote/MyVotes.bru b/tools/HttpRequests/Vote/MyVotes.bru new file mode 100644 index 0000000..b40ae47 --- /dev/null +++ b/tools/HttpRequests/Vote/MyVotes.bru @@ -0,0 +1,29 @@ +meta { + name: MyVotes + type: http + seq: 3 +} + +get { + url: {{java-url}}/votes/mine + body: none + auth: basic +} + +auth:basic { + username: {{username}} + password: {{password}} +} + +body:json { + { + "id": 1, + "score": 3, + "itsFor": { + "id": 1 + }, + "whoVotes": { + "id": 1 + } + } +} diff --git a/tools/HttpRequests/Vote/Results.bru b/tools/HttpRequests/Vote/Results.bru new file mode 100644 index 0000000..b0bfc09 --- /dev/null +++ b/tools/HttpRequests/Vote/Results.bru @@ -0,0 +1,29 @@ +meta { + name: Results + type: http + seq: 4 +} + +get { + url: {{java-url}}/votes/results + body: none + auth: basic +} + +auth:basic { + username: {{username}} + password: {{password}} +} + +body:json { + { + "id": 1, + "score": 3, + "itsFor": { + "id": 1 + }, + "whoVotes": { + "id": 1 + } + } +}