Skip to content

Commit

Permalink
update CmapaignService - add more business logic for other use cases
Browse files Browse the repository at this point in the history
  • Loading branch information
vladutmargineanu committed Jan 25, 2024
1 parent 26f6222 commit b9a7985
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "CAMPAIGN", schema = "matcherapplication")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@

@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "CLAN", schema = "matcherapplication")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "DEVICE", schema = "matcherapplication")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "INVENTORY", schema = "matcherapplication")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "ITEM", schema = "matcherapplication")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

@Getter
@Setter
@ToString
@NoArgsConstructor
@Entity
@Table(name = "PLAYER", schema = "matcherapplication")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

import java.util.Optional;

@Repository
public interface CampaignRepository extends JpaRepository<Campaign, Long> {
Optional<Campaign> findByNameAndEndDateIsNull(String name);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.profile.matcher.entity.campaign.Campaign;
import com.profile.matcher.entity.player.Player;
import com.profile.matcher.repository.CampaignRepository;
import com.profile.matcher.utils.DateHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.ParameterizedTypeReference;
Expand Down Expand Up @@ -94,37 +95,55 @@ public List<CampaignDto> getCurrentCampaignsMockedService() {
campaign.setEnabled(true);
campaign.setLast_updated("2021-07-13 11:46:58Z");
List<CampaignDto> campaignDtoList = Collections.singletonList(campaign);

writeLog("CampaignService.getCurrentCampaignsMockedService() current campaigns: {}", campaignDtoList);

return campaignDtoList;
}

/**
* Method used to create a campaign entity
* Method used to create a campaign entity if it does not exist
* If the campaign already exist ind db, we update the existing one
*
* @param campaignDto
* @param player
* @return Optional of campaign
*/
public Optional<Campaign> createCampaignEntity(CampaignDto campaignDto, Player player) {
if (null != campaignDto) {
writeLog("CampaignService.createCampaignEntity() entity from dto: {}", campaignDto);

Campaign campaign = new Campaign();
campaign.setGame(campaignDto.getGame());
campaign.setName(campaignDto.getName());
campaign.setPriority(campaignDto.getPriority());
campaign.setEnabled(campaignDto.getEnabled());
List<Player> players = new ArrayList<>();
players.add(player);
campaign.setPlayers(players);

writeLog("CampaignService.createCampaignEntity() campaign entity created: {}", campaign.getName());

return Optional.of(campaign);
public Optional<Campaign> getCampaignEntity(CampaignDto campaignDto, Player player) {
if (null != campaignDto && campaignDto.getEnabled()) {
writeLog("CampaignService.getCampaignEntity() entity from dto: {}", campaignDto);

Campaign campaign = null;
if (null != campaignDto.getName()) {
Optional<Campaign> campaignOptional = campaignRepository.findByNameAndEndDateIsNull(campaignDto.getName());

if (campaignOptional.isPresent()) {
campaign = campaignOptional.get();
writeLog("CampaignService.getCampaignEntity() campaign already exist in db: {}", campaign.getName());
campaign.getPlayers().add(player);
if (!campaignDto.getLast_updated().equals(DateHelper.toFormattedDateTimeString(campaign.getLastUpdated()))) {
setCampaignProperties(campaignDto, campaign);
}
} else {
campaign = new Campaign();
campaign.setName(campaignDto.getName());
List<Player> players = new ArrayList<>();
players.add(player);
campaign.setPlayers(players);
setCampaignProperties(campaignDto, campaign);
}
writeLog("CampaignService.getCampaignEntity() campaign entity created: {}", campaign.getName());

return Optional.of(campaign);
}
}

return Optional.empty();
}

private static void setCampaignProperties(CampaignDto campaignDto, Campaign campaign) {
campaign.setGame(campaignDto.getGame());
campaign.setPriority(campaignDto.getPriority());
campaign.setEnabled(campaignDto.getEnabled());
campaign.setLastUpdated(DateHelper.convertStringToTimestamp(campaignDto.getLast_updated()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ public class PlayerService extends BaseService {

/**
* Method to retrieve a player with id from database
* This method also update the retrieved player with the current campaign
* If the player satisfy some condition from current campaign
*
*
* @param idPlayer
* @return Optional of player
Expand Down Expand Up @@ -61,7 +64,7 @@ public Optional<Player> getPlayerDetails(String idPlayer) {
campaignDtoList.forEach(campaignDto -> {
if (matchCurrentCampaign(player, campaignDto) && checkNewCampaignForPlayer(player, campaignDto)) {

campaignService.createCampaignEntity(campaignDto, player).ifPresent(campaign -> {
campaignService.getCampaignEntity(campaignDto, player).ifPresent(campaign -> {
player.getCampaigns().add(campaign);
playerRepository.save(player);
writeLog("PlayerService.getPlayerDetails() - player updated with a new campaign: {}",
Expand Down
23 changes: 18 additions & 5 deletions matcher/src/main/java/com/profile/matcher/utils/DateHelper.java
Original file line number Diff line number Diff line change
@@ -1,17 +1,30 @@
package com.profile.matcher.utils;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import static com.profile.matcher.utils.Constants.DateTimePatterns.DATE_WITH_TIME;

public final class DateHelper {

public static String toFormattedDateTimeString(Timestamp dateTime) {
if (null == dateTime) {
public static String toFormattedDateTimeString(Timestamp timestamp) {
if (null == timestamp) {
return null;
}
SimpleDateFormat formatter = new SimpleDateFormat(DATE_WITH_TIME);
return formatter.format(dateTime);
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_WITH_TIME);
LocalDateTime localDateTime = timestamp.toLocalDateTime();

return formatter.format(localDateTime);
}

public static Timestamp convertStringToTimestamp(String dateString) {
if (null == dateString) {
return null;
}
DateTimeFormatter formatter = DateTimeFormatter.ofPattern(DATE_WITH_TIME);
LocalDateTime localDateTime = LocalDateTime.parse(dateString, formatter);

return Timestamp.valueOf(localDateTime);
}
}
1 change: 0 additions & 1 deletion resources/scripts/DDL/08_DDL_create_campaign_table.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,5 @@ CREATE TABLE `CAMPAIGN` (
`END_DATE` TIMESTAMP(6),

PRIMARY KEY (`ID_CAMPAIGN`),
UNIQUE KEY `GAME_UNIQUE` (`GAME`),
UNIQUE KEY `NAME_UNIQUE` (`NAME`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

0 comments on commit b9a7985

Please sign in to comment.