public async Task <Offerline> UpdateAsync(Offerline offerline) { var query = new QuerySet(); query.Include.Fields = new string[] { "offer", "vat-rate" }; var existingOfferline = await _offerlineDataProvider.GetByIdAsync(offerline.Id, query); if (offerline.Id != existingOfferline.Id) { throw new IllegalArgumentException("IllegalAttribute", "Offerline id cannot be updated."); } if (offerline.Amount == null) { throw new IllegalArgumentException("IllegalAttribute", "Amount is required on offerline."); } await EmbedRelations(offerline, existingOfferline); if (offerline.Offer == null) { throw new IllegalArgumentException("IllegalAttribute", "Offer is required."); } if (offerline.VatRate == null) { throw new IllegalArgumentException("IllegalAttribute", "Vat-rate is required."); } return(await _offerlineDataProvider.UpdateAsync(offerline)); }
public async Task <Offerline> CreateAsync(Offerline offerline) { if (offerline.Id != 0) { throw new IllegalArgumentException("IllegalAttribute", "Offerline cannot have an id on create."); } if (offerline.Offer == null) { throw new IllegalArgumentException("IllegalAttribute", "Offer is required on offerline creation."); } if (offerline.Amount == null) { throw new IllegalArgumentException("IllegalAttribute", "Amount is required on offerline creation."); } if (offerline.IsOrdered) { throw new IllegalArgumentException("IllegalAttribute", "Offerline cannot be ordered on creation."); } await EmbedRelations(offerline); if (offerline.Offer == null) { throw new IllegalArgumentException("IllegalAttribute", "Offer is required."); } if (offerline.VatRate == null) { throw new IllegalArgumentException("IllegalAttribute", "Vat-rate is required."); } return(await _offerlineDataProvider.CreateAsync(offerline)); }
// Embed relations in request resource: reuse old relation if there is one and it hasn't changed private async Task EmbedRelations(Offerline offerline, Offerline oldOfferline = null) { try { if (offerline.VatRate != null) { if (oldOfferline != null && oldOfferline.VatRate != null && oldOfferline.VatRate.Id == offerline.VatRate.Id) { offerline.VatRate = oldOfferline.VatRate; } else { offerline.VatRate = await _vatRateDataProvider.GetByIdAsync(int.Parse(offerline.VatRate.Id)); } } // Offer cannot be updated. Take Offer of oldOfferline on update. if (oldOfferline != null) { offerline.Offer = oldOfferline.Offer; } else { offerline.Offer = await _offerDataProvider.GetByIdAsync(offerline.Offer.Id); } } catch (EntityNotFoundException) { _logger.LogDebug($"Failed to find a related entity"); throw new IllegalArgumentException("IllegalAttribute", "Not all related entities exist."); } }
public async Task <Offerline> CreateAsync(Offerline offerline) { var offerlineRecord = _mapper.Map <DataProvider.Models.Offerline>(offerline); offerlineRecord.Currency = "EUR"; _context.Offerlines.Add(offerlineRecord); await _context.SaveChangesAsync(); return(_mapper.Map <Offerline>(offerlineRecord)); }
public async Task <Offerline> UpdateAsync(Offerline offerline) { var offerlineRecord = await FindByIdAsync(offerline.Id); _mapper.Map(offerline, offerlineRecord); offerlineRecord.Currency = "EUR"; _context.Offerlines.Update(offerlineRecord); await _context.SaveChangesAsync(); return(_mapper.Map <Offerline>(offerlineRecord)); }
public IEnumerable <IResource> CollectIncluded(Offerline offerline, IncludeQuery includeQuery) { ISet <IResource> included = new HashSet <IResource>(); // one-relations if (includeQuery.Contains("offer") && offerline.Offer != null) { included.Add(_mapper.Map <OfferDto>(offerline.Offer)); } if (includeQuery.Contains("vat-rate") && offerline.VatRate != null) { included.Add(_mapper.Map <VatRateDto>(offerline.VatRate)); } return(included); }
OfferlineRelationshipsDto ITypeConverter <Offerline, OfferlineRelationshipsDto> .Convert(Offerline source, OfferlineRelationshipsDto destination, ResolutionContext context) { var relationships = new OfferlineRelationshipsDto(); relationships.Offer = GetOneRelationship <Offer>("offerlines", source.Id, "offer", source.Offer, context); relationships.VatRate = GetOneRelationship <VatRate>("offerlines", source.Id, "vat-rate", source.VatRate, context); return(relationships); }