public async Task SendExplorersToCountry(SendExplorationDTO explorationDTO) { if (explorationDTO.SenderCountryID == explorationDTO.VictimCountryID) { throw new HttpResponseException { Status = 400, Value = "Ne bántsd magad :c" } } ; SendExplorationDTOValidator explorationValidator = new SendExplorationDTOValidator(); ValidationResult validatorResults = explorationValidator.Validate(explorationDTO); if (!validatorResults.IsValid) { foreach (var failure in validatorResults.Errors) { throw new HttpResponseException { Status = 400, Value = failure.ErrorMessage }; } } int allExplorers = 0; var foundExplorers = await _context.Units.SingleOrDefaultAsync(u => u.CountryID == explorationDTO.SenderCountryID && u.UnitDataID == UnitData.Explorer.ID); if (foundExplorers != null) { allExplorers = foundExplorers.Count; } var exploring = await _context.Explorations.Where(e => e.SenderCountryID == explorationDTO.SenderCountryID).SumAsync(e => e.NumberOfExplorers); var availableExplorers = allExplorers - exploring; if (availableExplorers < explorationDTO.NumberOfExplorers) { throw new HttpResponseException { Status = 400, Value = "Nincs elég felfedező" }; } var existingExp = await _context.Explorations.SingleOrDefaultAsync(e => e.SenderCountryID == explorationDTO.SenderCountryID && e.VictimCountryID == explorationDTO.VictimCountryID); if (existingExp == null) { var newExploration = new Exploration() { NumberOfExplorers = explorationDTO.NumberOfExplorers, SenderCountryID = explorationDTO.SenderCountryID, VictimCountryID = explorationDTO.VictimCountryID }; _context.Explorations.Add(newExploration); } else { existingExp.NumberOfExplorers += explorationDTO.NumberOfExplorers; } await _context.SaveChangesAsync(); }
public async Task <ActionResult> Explore([FromBody] SendExplorationDTO explorationDTO) { var atkCountry = await _userService.GetCountryByUserID(User.Identity.Name); explorationDTO.SenderCountryID = atkCountry.ID; await _battleService.SendExplorersToCountry(explorationDTO); return(Ok()); }