public async Task <int> Handle(UpdateRelationCommand request, CancellationToken cancellationToken) { var entity = await _context.Relations.SingleOrDefaultAsync(x => x.Id == request.Id, cancellationToken); _ = entity ?? throw new NotFoundException(nameof(Relation), request.Id); var entry = _context.Entry(entity); entry.CurrentValues.SetValues(request); Occurrence occurrence1; if (request.Occurrence1IsGeneric) { occurrence1 = await _context.GenericOccurrences.SingleOrDefaultAsync(x => x.Id == request.Occurrence1Id, cancellationToken); _ = occurrence1 ?? throw new NotFoundException(nameof(GenericOccurrence), occurrence1); } else { occurrence1 = await _context.SpecificOccurrences.SingleOrDefaultAsync(x => x.Id == request.Occurrence1Id, cancellationToken); _ = occurrence1 ?? throw new NotFoundException(nameof(SpecificOccurrence), occurrence1); } entity.Occurrence1 = occurrence1; Occurrence occurrence2; if (request.Occurrence2IsGeneric) { occurrence2 = await _context.GenericOccurrences.SingleOrDefaultAsync(x => x.Id == request.Occurrence2Id, cancellationToken); _ = occurrence2 ?? throw new NotFoundException(nameof(GenericOccurrence), occurrence2); } else { occurrence2 = await _context.SpecificOccurrences.SingleOrDefaultAsync(x => x.Id == request.Occurrence2Id, cancellationToken); _ = occurrence2 ?? throw new NotFoundException(nameof(SpecificOccurrence), occurrence2); } entity.Occurrence2 = occurrence2; var relationType = await _context.RelationTypes.SingleOrDefaultAsync(x => x.Id == request.RelationTypeId, cancellationToken); _ = relationType ?? throw new NotFoundException(nameof(RelationType), relationType); entity.RelationType = relationType; await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }
public async Task <int> Handle(CreateGenericOccurrenceCommand request, CancellationToken cancellationToken) { var entity = new GenericOccurrence(request.Name); _context.GenericOccurrences.Add(entity); _context.Entry(entity).CurrentValues.SetValues(request); entity.AdditionalData = request.AdditionalData; await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }
public async Task <Unit> Handle(CreateCategoryCommand request, CancellationToken cancellationToken) { var entity = new Category(request.Name); _context.Categories.Add(entity); var entry = _context.Entry(entity); entry.CurrentValues.SetValues(request); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <int> Handle(UpdateCategoryCommand request, CancellationToken cancellationToken) { var entity = await _context.Categories.SingleOrDefaultAsync(x => x.Id == request.Id, cancellationToken); _ = entity ?? throw new NotFoundException(nameof(Category), request.Id); var entry = _context.Entry(entity); entry.CurrentValues.SetValues(request); await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }
public async Task <int> Handle(UpdateSpecificOccurrenceCommand request, CancellationToken cancellationToken) { var entity = await _context.SpecificOccurrences.SingleOrDefaultAsync(x => x.Id == request.Id, cancellationToken); _ = entity ?? throw new NotFoundException(nameof(SpecificOccurrence), request.Id); var entry = _context.Entry(entity); entry.CurrentValues.SetValues(request); var occurrenceType = await _context.GenericOccurrences.SingleOrDefaultAsync(x => x.Id == request.OccurrenceTypeId, cancellationToken); _ = occurrenceType ?? throw new NotFoundException(nameof(GenericOccurrences), request.OccurrenceTypeId); entity.OccurrenceType = occurrenceType; await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }