Skip to content

Commit

Permalink
Merge pull request #3 from dangerousplay/improve-raffle-participant
Browse files Browse the repository at this point in the history
[Improved] Raffle participant feature performance
  • Loading branch information
myghi63 authored Aug 4, 2024
2 parents fcaec28 + b444a4c commit 327e647
Show file tree
Hide file tree
Showing 6 changed files with 287 additions and 63 deletions.
16 changes: 16 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: Compile and test
run-name: Compile and test ${{ github.actor }}
on: [push]
jobs:
compile-and-test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up JDK 11
uses: actions/setup-java@v4
with:
java-version: '17'
distribution: 'temurin'
cache: maven
- name: Build with Maven
run: mvn --batch-mode --update-snapshots verify
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.cookiebot.cookiebotbackend.core.domains;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.Document;

@Document(collection = "raffles")
public class Raffle implements Serializable {
private static final long serialVersionUID = 1L;
public class Raffle {

@Id
private String name;
Expand Down Expand Up @@ -59,4 +58,22 @@ public List<RaffleParticipant> getParticipants() {
public void setParticipants(List<RaffleParticipant> participants) {
this.participants = participants;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Raffle raffle = (Raffle) o;

return Objects.equals(name, raffle.name) &&
Objects.equals(award, raffle.award) &&
Objects.equals(deadline, raffle.deadline) &&
Objects.equals(participants, raffle.participants);
}

@Override
public int hashCode() {
return Objects.hash(name, award, deadline, participants);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.cookiebot.cookiebotbackend.core.domains;

import java.io.Serializable;
import java.util.Objects;

public class RaffleParticipant {
public static final String USER_FIELD = "user";

public class RaffleParticipant implements Serializable {
private static final long serialVersionUID = 1L;

private String user;

public RaffleParticipant() {
Expand All @@ -22,4 +22,17 @@ public String getUser() {
public void setUser(String user) {
this.user = user;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
RaffleParticipant that = (RaffleParticipant) o;
return Objects.equals(user, that.user);
}

@Override
public int hashCode() {
return Objects.hashCode(user);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.data.mongodb.core.MongoOperations;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;

import com.cookiebot.cookiebotbackend.core.domains.Raffle;
Expand All @@ -16,26 +21,30 @@
@Service
public class RaffleService {

@Autowired
private RaffleRepository repository;

private final RaffleRepository repository;

private final MongoOperations mongoOperations;

public RaffleService(RaffleRepository repository, MongoOperations mongoOperations) {
this.repository = repository;
this.mongoOperations = mongoOperations;
}

public List<Raffle> findAll(){
return repository.findAll();
}

public Raffle findByName(String name) {
Raffle raffle = repository.findById(name)
return repository.findById(name)
.orElseThrow(() -> new ObjectNotFoundException("Object Not Found"));
return raffle;
}

public Raffle insert(String name, Raffle raffle) {
if (repository.findById(name).orElse(null) != null) {
throw new BadRequestException("Name Already Exists");
}

raffle.setName(name);
return repository.insert(raffle);
public Raffle insert(Raffle raffle) {
try {
return repository.insert(raffle);
} catch (DuplicateKeyException e) {
throw new BadRequestException("Raffle with name " + raffle.getName() + " already exists");
}
}

public void delete(String name) {
Expand All @@ -61,58 +70,44 @@ private void updateRaffle(Raffle newRaffle, Raffle raffle) {
}

public List<RaffleParticipant> findParticipants(String name) {
Raffle raffle = repository.findById(name)
final Raffle raffle = repository.findById(name)
.orElseThrow(() -> new ObjectNotFoundException("Object Not Found"));
List<RaffleParticipant> participantList = raffle.getParticipants();
return participantList;

return raffle.getParticipants();
}

public void insertParticipant(String name, RaffleParticipant participant) {
public void insertParticipant(String raffleId, RaffleParticipant participant) {
if (participant.getUser() == null) {
throw new BadRequestException("User Must Not Be Null");
}

Raffle raffle = repository.findById(name)
.orElseThrow(() -> new ObjectNotFoundException("Object Not Found"));
List<RaffleParticipant> participantList = new ArrayList<RaffleParticipant>(raffle.getParticipants());

Integer participantSize = participantList.size();

for (int participantArray = participantSize-1; participantArray >= 0; participantArray--) {
if (participantList.get(participantArray).getUser().matches(participant.getUser())){
throw new BadRequestException("User Already Exists");
}
}

participantList.addAll(Arrays.asList(participant));
raffle.setParticipants(participantList);
repository.save(raffle);
repository.findById(raffleId)
.orElseThrow(() -> new ObjectNotFoundException("Not found raffled with id " + raffleId));

final Query query = new Query(Criteria.where("_id").is(raffleId));
final Update update = new Update().addToSet("participants", participant);

this.mongoOperations.updateFirst(query, update, Raffle.class);
}

public void deleteParticipant(String name, RaffleParticipant participant) {
public void deleteParticipant(String raffleId, RaffleParticipant participant) {
if (participant.getUser() == null) {
throw new BadRequestException("User Must Not Be Null");
}

Raffle raffle = repository.findById(name)
.orElseThrow(() -> new ObjectNotFoundException("Object Not Found"));
List<RaffleParticipant> participantList = new ArrayList<RaffleParticipant>(raffle.getParticipants());
String participantToDelete = participant.getUser();
Integer participantSize = participantList.size();

boolean foundParticipant = false;
for (int participantArray = participantSize-1; participantArray >= 0; participantArray--) {
if (participantList.get(participantArray).getUser().matches(participantToDelete)){
foundParticipant = true;
participantList.remove(participantArray);
raffle.setParticipants(participantList);
repository.save(raffle);
}
}

if (foundParticipant == false) {
throw new ObjectNotFoundException("Participant Not Found");
}

repository.findById(raffleId)
.orElseThrow(() -> new ObjectNotFoundException("Not found raffled with id " + raffleId));

final Criteria filterUser = Criteria.where(RaffleParticipant.USER_FIELD).is(participant.getUser());

final Query query = new Query(
Criteria.where("_id").is(raffleId)
.and("participants").elemMatch(filterUser)
);

final Update update = new Update().pull("participants", Map.of(RaffleParticipant.USER_FIELD, participant.getUser()));

this.mongoOperations.updateFirst(query, update, Raffle.class);
}

public void updateParticipant(String name, RaffleParticipant participant) {
Expand All @@ -122,7 +117,7 @@ public void updateParticipant(String name, RaffleParticipant participant) {

Raffle raffle = repository.findById(name)
.orElseThrow(() -> new ObjectNotFoundException("Object Not Found"));
List<RaffleParticipant> participantList = new ArrayList<RaffleParticipant>(raffle.getParticipants());
List<RaffleParticipant> participantList = new ArrayList<>(raffle.getParticipants());
Integer participantSize = participantList.size();

boolean foundParticipant = false;
Expand All @@ -136,7 +131,7 @@ public void updateParticipant(String name, RaffleParticipant participant) {
}
}

if (foundParticipant == false) {
if (!foundParticipant) {
throw new ObjectNotFoundException("User Not Found");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ public ResponseEntity<Raffle> findByName(@PathVariable String name) {

@PostMapping(value = "/{name}")
public ResponseEntity<Raffle> insert(@RequestBody Raffle raffle, @PathVariable String name) {
service.insert(name, raffle);
raffle.setName(name);

service.insert(raffle);

URI uri = ServletUriComponentsBuilder.fromCurrentRequest()
.path("/{name}").buildAndExpand(name).toUri();

return ResponseEntity.created(uri).build();
}

Expand Down
Loading

0 comments on commit 327e647

Please sign in to comment.