Skip to content

Commit

Permalink
Feat: Improving Vote managing
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Gonçalves committed Jan 18, 2024
1 parent 09323a8 commit 141e711
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 2 deletions.
39 changes: 39 additions & 0 deletions src/main/java/br/com/darksun/controller/VoteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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( );
}
}
5 changes: 5 additions & 0 deletions src/main/java/br/com/darksun/repository/VoteRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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( );
}
}
24 changes: 24 additions & 0 deletions src/main/java/br/com/darksun/service/VoteService.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<>( );
Expand All @@ -63,13 +69,31 @@ 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 ) {
throw new EntityNotFoundException( "Vote not found with ID: " + 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( ) ) );
Expand Down
29 changes: 29 additions & 0 deletions tools/HttpRequests/Vote/ChangeScore.bru
Original file line number Diff line number Diff line change
@@ -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
}
}
}
2 changes: 1 addition & 1 deletion tools/HttpRequests/Vote/Create.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: Create
type: http
seq: 4
seq: 5
}

post {
Expand Down
2 changes: 1 addition & 1 deletion tools/HttpRequests/Vote/Delete.bru
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
meta {
name: Delete
type: http
seq: 5
seq: 7
}

delete {
Expand Down
29 changes: 29 additions & 0 deletions tools/HttpRequests/Vote/DeleteMyVote.bru
Original file line number Diff line number Diff line change
@@ -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
}
}
}
29 changes: 29 additions & 0 deletions tools/HttpRequests/Vote/MyVotes.bru
Original file line number Diff line number Diff line change
@@ -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
}
}
}
29 changes: 29 additions & 0 deletions tools/HttpRequests/Vote/Results.bru
Original file line number Diff line number Diff line change
@@ -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
}
}
}

0 comments on commit 141e711

Please sign in to comment.