Skip to content

Commit

Permalink
Merge pull request #24 from bouanani-soufiane/PIG-30-gestion-des-resu…
Browse files Browse the repository at this point in the history
…ltats

Pig 30 gestion des resultats
  • Loading branch information
youssefhihi authored Nov 15, 2024
2 parents 7ca4091 + edb2f0b commit 899c0a3
Show file tree
Hide file tree
Showing 40 changed files with 414 additions and 60 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package ma.yc.PigeonSkyRace.common.application.service;

import ma.yc.PigeonSkyRace.competition.domain.ValueObject.Coordinate;
import org.springframework.stereotype.Component;

@Component
public class Helper {

public static double calculateDistance(Coordinate loftCoordinate, Coordinate competitionCoordinate) {

double loftLatRad = Math.toRadians(loftCoordinate.latitude());
double loftLonRad = Math.toRadians(loftCoordinate.longitude());
double competitionLatRad = Math.toRadians(competitionCoordinate.latitude());
double competitionLonRad = Math.toRadians(competitionCoordinate.longitude());

double deltaLat = competitionLatRad - loftLatRad;
double deltaLon = competitionLonRad - loftLonRad;
double a = Math.sin(deltaLat / 2) * Math.sin(deltaLat / 2)
+ Math.cos(loftLatRad) * Math.cos(competitionLatRad)
* Math.sin(deltaLon / 2) * Math.sin(deltaLon / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

return 6371.01 * c;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ class CompetitionController {
private final CompetitionService competitionService;

@PostMapping
public ResponseEntity<ApiResponse<CompetitionResponseDto>> createCompetition ( @Valid @RequestBody CompetitionRequestDto competition ) {
public ResponseEntity<ApiResponse<CompetitionResponseDto>> createCompetition(@Valid @RequestBody CompetitionRequestDto competition) {
CompetitionResponseDto createdCompetition = competitionService.createCompetition(competition);
ApiResponse<CompetitionResponseDto> response = new ApiResponse<>(createdCompetition, "Competition created successfully", HttpStatus.CREATED);
return new ResponseEntity<>(response, HttpStatus.CREATED);
}


@GetMapping
public ResponseEntity<List<CompetitionResponseDto>> getAllCompetitions () {
public ResponseEntity<List<CompetitionResponseDto>> getAllCompetitions() {
List<CompetitionResponseDto> competitions = competitionService.getAllCompetitions();
return new ResponseEntity<>(competitions, HttpStatus.OK);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Positive;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.AdmissionPercentage;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.Coordinate;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.SeasonId;

import java.time.LocalDateTime;
Expand All @@ -12,7 +13,7 @@ public record CompetitionRequestDto(
@NotBlank String name,
@NotBlank String description,
@Positive Integer maxPigeons,
@Positive Double distance,
@NotNull Coordinate coordinate,
@NotNull AdmissionPercentage admissionPercentage,
@NotNull LocalDateTime dateStart,
@NotNull LocalDateTime dateEnd,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package ma.yc.PigeonSkyRace.competition.application.dto.request;

import jakarta.validation.constraints.NotNull;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.SeasonId;
import ma.yc.PigeonSkyRace.competition.domain.entity.Season;
import ma.yc.PigeonSkyRace.piegon.domain.model.aggregate.Pigeon;
import ma.yc.PigeonSkyRace.piegon.domain.model.valueObject.PigeonId;

public record SeasonPigeonRequestDto(
@NotNull Pigeon pigeon,
@NotNull Season season
) {
public record SeasonPigeonRequestDto(@NotNull Pigeon pigeon ,@NotNull Season season) {
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@
import ma.yc.PigeonSkyRace.competition.domain.entity.Season;
import ma.yc.PigeonSkyRace.piegon.domain.model.aggregate.Pigeon;

import java.time.LocalDateTime;

public record SeasonPigeonResponseDto (
public record SeasonPigeonResponseDto(
SeasonPigeonId id,
Pigeon pigeon,
Season season,
LocalDateTime createdDate

){
Season season
) {
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
@Mapper(componentModel = "spring")
public interface CompetitionPigeonMapper {
@Mapping(target = "createdDate", expression = "java(java.time.LocalDateTime.now())")
CompetitionPigeon toEntity(CompetitionPigeonRequestDto seasonPigeonRequestDto);
CompetitionPigeon toEntity(CompetitionPigeonRequestDto competitionPigeonRequestDto);

CompetitionPigeonResponseDto toDto(CompetitionPigeon seasonPigeon);
CompetitionPigeonResponseDto toDto(CompetitionPigeon competitionPigeon);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package ma.yc.PigeonSkyRace.competition.application.service;

import ma.yc.PigeonSkyRace.competition.application.dto.response.CompetitionResponseDto;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.CompetitionId;
import ma.yc.PigeonSkyRace.competition.domain.entity.Competition;

import java.time.LocalDateTime;

public interface CompetitionApplicationService {
CompetitionResponseDto getCompetition(CompetitionId id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package ma.yc.PigeonSkyRace.competition.application.service;

import ma.yc.PigeonSkyRace.competition.domain.ValueObject.CompetitionPigeonId;
import ma.yc.PigeonSkyRace.competition.domain.entity.Competition;
import ma.yc.PigeonSkyRace.competition.domain.entity.CompetitionPigeon;
import ma.yc.PigeonSkyRace.competition.domain.entity.SeasonPigeon;

import javax.swing.text.html.Option;
import java.util.Optional;

public interface CompetitionPigeonApplicationService {
CompetitionPigeon findBySeasonPigeonAndCompetition(SeasonPigeon seasonPigeon, Competition competition);
CompetitionPigeon findById(CompetitionPigeonId id);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package ma.yc.PigeonSkyRace.competition.application.service;

import ma.yc.PigeonSkyRace.competition.domain.entity.Season;
import ma.yc.PigeonSkyRace.competition.domain.entity.SeasonPigeon;
import ma.yc.PigeonSkyRace.piegon.domain.model.aggregate.Pigeon;

public interface SeasonPigeonApplicationService {
SeasonPigeon findSeasonPigeonPigeonAndSeason(Season season, Pigeon pigeon);
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

import jakarta.validation.constraints.NotNull;

public record Coordinate(@NotNull Double latitude, @NotNull Double longitude) {
public record Coordinate( Double latitude, Double longitude) {
public Coordinate {
if (latitude < -90 || latitude > 90) {
throw new IllegalArgumentException("Latitude must be between -90 and 90 degrees.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ public class Competition {
@NotNull
private SeasonId seasonId;



@CreatedDate
private LocalDateTime createdDate;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,13 @@ public class Season {
@NotBlank
private String description;


@Field(name = "is_active")
private Boolean isActive;

@DBRef
private List<SeasonPigeon> seasonPigeons = new ArrayList<>();

@DBRef
@DBRef()
private List<Competition> competitions = new ArrayList<>();

@CreatedDate
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package ma.yc.PigeonSkyRace.competition.domain.service;
import ma.yc.PigeonSkyRace.competition.application.dto.response.CompetitionPigeonResponseDto;
import ma.yc.PigeonSkyRace.competition.application.dto.response.CompetitionResponseDto;
import ma.yc.PigeonSkyRace.competition.application.dto.response.SeasonPigeonResponseDto;
import ma.yc.PigeonSkyRace.competition.domain.entity.Competition;
import ma.yc.PigeonSkyRace.competition.domain.entity.SeasonPigeon;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package ma.yc.PigeonSkyRace.competition.domain.service.Impl;

import lombok.RequiredArgsConstructor;
import ma.yc.PigeonSkyRace.common.domain.exception.NotFoundException;
import ma.yc.PigeonSkyRace.competition.application.dto.request.CompetitionPigeonRequestDto;
import ma.yc.PigeonSkyRace.competition.application.dto.response.CompetitionPigeonResponseDto;
import ma.yc.PigeonSkyRace.competition.application.mapping.CompetitionPigeonMapper;
import ma.yc.PigeonSkyRace.competition.application.service.CompetitionPigeonApplicationService;
import ma.yc.PigeonSkyRace.competition.domain.Exception.FailedToRegister;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.Coordinate;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.CompetitionPigeonId;
import ma.yc.PigeonSkyRace.competition.domain.entity.Competition;
import ma.yc.PigeonSkyRace.competition.domain.entity.CompetitionPigeon;
import ma.yc.PigeonSkyRace.competition.domain.entity.SeasonPigeon;
Expand All @@ -16,11 +18,12 @@

import java.time.LocalDateTime;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CompetitionPigeonDomainService implements CompetitionPigeonService {
public class CompetitionPigeonDomainService implements CompetitionPigeonService , CompetitionPigeonApplicationService {

private final CompetitionPigeonRepository repository;
private final CompetitionPigeonMapper mapper;
Expand Down Expand Up @@ -59,19 +62,19 @@ public List<CompetitionPigeonResponseDto> getAllCompetitionsPigeons(){
}


private Coordinate calculateAverageCoordinate(List<CompetitionPigeon> competitionPigeons) {
double sumLatitude = 0;
double sumLongitude = 0;
int count = competitionPigeons.size();

for (CompetitionPigeon pigeon : competitionPigeons) {
Coordinate pigeonCoordinate = applicationService.geLoftCoordinate(pigeon.getSeasonPigeon().getPigeon().getLoft());
sumLatitude += pigeonCoordinate.latitude();
sumLongitude += pigeonCoordinate.longitude();
}
@Override
public CompetitionPigeon findBySeasonPigeonAndCompetition(SeasonPigeon seasonPigeon, Competition competition) {
return repository.findBySeasonPigeonAndCompetition(
seasonPigeon, competition
).orElseThrow( () ->
new NotFoundException("Competition-Pigeon",seasonPigeon.getSeason().getId())
);
}

return new Coordinate(sumLatitude / count, sumLongitude / count);
@Override
public CompetitionPigeon findById(CompetitionPigeonId id){
return repository.findById(id).orElseThrow(() -> new NotFoundException("competitionPigeon", id));
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,24 @@
import ma.yc.PigeonSkyRace.competition.application.dto.request.CompetitionRequestDto;
import ma.yc.PigeonSkyRace.competition.application.dto.response.CompetitionResponseDto;
import ma.yc.PigeonSkyRace.competition.application.events.CompetitionCreatedEvent;
import ma.yc.PigeonSkyRace.competition.application.service.CompetitionApplicationService;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.CompetitionId;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.Coordinate;
import ma.yc.PigeonSkyRace.competition.domain.entity.Competition;
import ma.yc.PigeonSkyRace.competition.domain.service.CompetitionService;
import ma.yc.PigeonSkyRace.competition.infrastructure.repository.CompetitionRepository;
import ma.yc.PigeonSkyRace.competition.application.mapping.CompetitionMapper;
import org.springframework.beans.factory.annotation.Autowired;
import static ma.yc.PigeonSkyRace.common.application.service.Helper.calculateDistance;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class CompetitionServiceImpl implements CompetitionService {
public class CompetitionServiceImpl implements CompetitionService, CompetitionApplicationService {

private final CompetitionRepository repository;
private final CompetitionMapper mapper;
Expand All @@ -30,6 +32,7 @@ public class CompetitionServiceImpl implements CompetitionService {
@Override
public CompetitionResponseDto createCompetition(CompetitionRequestDto competitionRequestDto) {
Competition competition = mapper.toEntity(competitionRequestDto);
competition.setDistance(calculateDistance(competition.getCoordinate(),new Coordinate(32.2994,-9.2372)));
Competition savedCompetition = repository.save(competition);
eventPublisher.publishEvent(new CompetitionCreatedEvent(savedCompetition, competitionRequestDto.seasonId()));
return mapper.toDto(savedCompetition);
Expand Down Expand Up @@ -58,4 +61,5 @@ public Boolean updateCompetition(CompetitionId id, Coordinate coordinate) {
throw new NotFoundException("Competition", id);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,19 @@
import ma.yc.PigeonSkyRace.competition.application.dto.request.SeasonPigeonRequestDto;
import ma.yc.PigeonSkyRace.competition.application.dto.response.SeasonPigeonResponseDto;
import ma.yc.PigeonSkyRace.competition.application.mapping.SeasonPigeonMapper;
import ma.yc.PigeonSkyRace.competition.application.service.SeasonPigeonApplicationService;
import ma.yc.PigeonSkyRace.competition.domain.Exception.FailedToRegister;
import ma.yc.PigeonSkyRace.competition.domain.ValueObject.SeasonPigeonId;
import ma.yc.PigeonSkyRace.competition.domain.entity.Season;
import ma.yc.PigeonSkyRace.competition.domain.entity.SeasonPigeon;
import ma.yc.PigeonSkyRace.competition.infrastructure.repository.SeasonPigeonRepository;
import ma.yc.PigeonSkyRace.competition.domain.service.SeasonPigeonService;
import ma.yc.PigeonSkyRace.piegon.domain.model.aggregate.Pigeon;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class SeasonPigeonDomainService implements SeasonPigeonService {
public class SeasonPigeonDomainService implements SeasonPigeonService , SeasonPigeonApplicationService {
private final SeasonPigeonRepository repository;
private final SeasonPigeonMapper mapper;

Expand All @@ -39,5 +42,12 @@ public SeasonPigeonResponseDto getSeasonById(SeasonPigeonId seasonPigeonId){
}



@Override
public SeasonPigeon findSeasonPigeonPigeonAndSeason(Season season, Pigeon pigeon) {
return repository.findByPigeonAndSeason(
pigeon, season
).orElseThrow(() ->
new NotFoundException("pigeonSeason", pigeon.getId())
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@
import org.springframework.data.mongodb.repository.Query;
import org.springframework.data.mongodb.repository.Update;

import java.time.LocalDateTime;
import java.util.Optional;

public interface CompetitionRepository extends MongoRepository<Competition, CompetitionId> {

@Query("{ 'id': ?0 }")
@Update("{ '$set': { 'coordinate': ?1 }}")
void updateCoordinateById(CompetitionId id, Coordinate coordinate);
Optional<Competition> findFirstByDateStartBeforeAndDateEndAfter(LocalDateTime startTime, LocalDateTime endTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,13 @@ public ResponseEntity<ResponseApi<PigeonResponseDTO>> findById ( @Valid @PathVar
.status(HttpStatus.OK)
.body(ResponseApi.success(pigeonResponse, "Successfully retrieved the pigeon by id."));
}

@DeleteMapping("{id}")
public ResponseEntity<String> delete ( @Valid @PathVariable String id ) {
Boolean dd = service.deletePigeonById(PigeonId.fromString(id));
if (dd) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body("Pigeon deleted successfully.");
}
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Pigeon could not be deleted.");
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ma.yc.PigeonSkyRace.piegon.application.service;

import ma.yc.PigeonSkyRace.competition.domain.ValueObject.Coordinate;
import ma.yc.PigeonSkyRace.piegon.domain.model.entity.Loft;
import ma.yc.PigeonSkyRace.piegon.domain.model.valueObject.LoftId;

public interface LoftApplicationService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package ma.yc.PigeonSkyRace.piegon.application.service;

import ma.yc.PigeonSkyRace.piegon.domain.model.aggregate.Pigeon;
import ma.yc.PigeonSkyRace.piegon.domain.model.valueObject.BandNumber;
import ma.yc.PigeonSkyRace.piegon.domain.model.valueObject.PigeonId;

public interface PigeonApplicationService {
Pigeon findPigeonById ( PigeonId value );
Pigeon findPigeonByBandNumber ( BandNumber value );
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import ma.yc.PigeonSkyRace.piegon.domain.model.valueObject.PigeonId;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.index.Indexed;
import org.springframework.data.mongodb.core.mapping.DBRef;
import org.springframework.data.mongodb.core.mapping.Document;

Expand All @@ -28,6 +29,7 @@ public class Pigeon {
private PigeonId id;

@NotNull
@Indexed(unique = true)
private BandNumber bandNumber;

@NotNull
Expand Down
Loading

0 comments on commit 899c0a3

Please sign in to comment.