public async Task <GetLivestockForEditOutput> GetLivestockForEdit(NullableIdDto <int> input) { var officer = _officerRepository.FirstOrDefault(x => x.UserId == AbpSession.UserId); if (officer == null) { throw new UserFriendlyException(L("TheOfficerDoesNotExists")); } Livestock livestock = null; if (input.Id.HasValue) { livestock = await _livestockRepository.GetAll() .Include(x => x.Officer) .FirstOrDefaultAsync(x => x.Id == input.Id.Value); } var output = new GetLivestockForEditOutput(); //livestock var newLiveStock = new LivestockCreateOrUpdateInput(); newLiveStock.CreationTime = newLiveStock.CreationTime.GetShamsi(); output.Livestock = livestock != null ? ObjectMapper.Map <LivestockCreateOrUpdateInput>(livestock) : newLiveStock; //FirmTypes output.SpeciesInfos = _speciesInfoRepository .GetAllList() .Select(c => new ComboboxItemDto(c.Id.ToString(), c.Code + " - " + c.Name)) .ToList(); output.SexInfos = _sexInfoRepository .GetAllList() .Select(c => new ComboboxItemDto(c.Id.ToString(), c.Name)) .ToList(); output.Herds = _herdRepository .GetAllList() .Where(x => x.OfficerId == officer.Id && !x.IsCertificated) .Select(c => new ComboboxItemDto(c.Id.ToString(), c.Code + " - " + c.HerdName + "(" + c.Name + "," + c.Family + ")")) .ToList(); if (output.Livestock.HerdId.HasValue) { output.ActivityInfos = _herdRepository.GetAll() .Include(x => x.ActivityInfo) .Where(x => x.Id == output.Livestock.HerdId) .Select(c => new ComboboxItemDto(c.ActivityInfo.Id.ToString(), c.ActivityInfo.Name)) .ToList(); } return(output); }
public async Task CreateOrUpdateLivestock(LivestockCreateOrUpdateInput input) { await CheckValidation(input); if (input.Id.HasValue) { await UpdateLivestockAsync(input); } else { await CreateLivestockAsync(input); } }
public async Task <LivestockCreateOrUpdateInput> CheckValidation(LivestockCreateOrUpdateInput input) { var species = await _speciesInfoRepository.GetAsync(input.SpeciesInfoId.Value); long nationalCode = Convert.ToInt64(input.NationalCode); if (nationalCode < species.FromCode) { nationalCode += species.FromCode; } if (nationalCode < species.FromCode || nationalCode > species.ToCode) { throw new UserFriendlyException(L("ThisCodeRangeShouldBe", species.Name, species.FromCode, species.ToCode)); } var plaqueInfo = _plaqueInfoRepository.FirstOrDefault(x => x.Code == nationalCode && x.StateId != 5); if (plaqueInfo != null) { throw new UserFriendlyException(L("ThisCodeIsAllocated", nationalCode)); } var officer = _officerRepository.FirstOrDefault(x => x.UserId == AbpSession.UserId); if (officer == null) { throw new UserFriendlyException(L("TheOfficerDoesNotExists")); } else { input.OfficerId = officer.Id; var plaqueToOfficer = _plaqueToOfficerRepository.FirstOrDefault(x => x.FromCode <= nationalCode && x.ToCode >= nationalCode); if (plaqueToOfficer == null) { throw new UserFriendlyException(L("ThisStoreIsNotAllocatedTo", nationalCode)); } } input.NationalCode = nationalCode.ToString(); return(input); }
private async Task CreateLivestockAsync(LivestockCreateOrUpdateInput input) { var livestock = ObjectMapper.Map <Livestock>(input); await _livestockRepository.InsertAsync(livestock); await CurrentUnitOfWork.SaveChangesAsync(); var plaqueInfo = new PlaqueInfo { Code = Convert.ToInt64(livestock.NationalCode), SetTime = livestock.CreationTime, Latitude = livestock.Latitude, Longitude = livestock.Longitude, OfficerId = livestock.OfficerId, StateId = 1, LivestockId = livestock.Id }; await _plaqueInfoRepository.InsertAsync(plaqueInfo); }
private async Task UpdateLivestockAsync(LivestockCreateOrUpdateInput input) { var livestock = ObjectMapper.Map <Livestock>(input); await _livestockRepository.UpdateAsync(livestock); }