public OrganisationDTO(Models.Database.Organisation organisation, OrganisationCounter orgCounter, int total) { Id = organisation.Id.ToString(); Balance = orgCounter?.Balance ?? 0; Name = organisation.Name; Total = total; }
public async Task UpdateCountAsync(string id, string deviceId, UpdateType updateType) { var balance = 0; var organisation = await _organisationRepository.GetAsync(Guid.Parse(id)); if (organisation == default) { throw new NotFoundException(Messages.Org_NotExists); } var lastCount = await _organisationCounterRepository.GetLastByOrganisation(organisation); balance = lastCount?.Balance ?? 0; if (balance < 1 && updateType == UpdateType.Subtraction) { throw new ValidationException(Messages.Org_NegBalance); } var newCount = new OrganisationCounter() { Organisation = organisation, Date = DateTime.UtcNow, Movement = updateType == UpdateType.Addition ? 1 : -1, Balance = updateType == UpdateType.Addition ? balance + 1 : balance - 1 , DeviceIdentifier = deviceId }; await _organisationCounterRepository.AddAsync(newCount); await _organisationCounterRepository.SaveAsync(); }