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(CreateRelationCommand request, CancellationToken cancellationToken) { Occurrence occurrence1; Occurrence occurrence2; if (request.Occurrence1IsGeneric) { occurrence1 = await _context.GenericOccurrences.SingleOrDefaultAsync(x => x.Id == request.Occurrence1Id, cancellationToken); } else { occurrence1 = await _context.SpecificOccurrences.SingleOrDefaultAsync(x => x.Id == request.Occurrence1Id, cancellationToken); } if (request.Occurrence2IsGeneric) { occurrence2 = await _context.GenericOccurrences.SingleOrDefaultAsync(x => x.Id == request.Occurrence2Id, cancellationToken); } else { occurrence2 = await _context.SpecificOccurrences.SingleOrDefaultAsync(x => x.Id == request.Occurrence2Id, cancellationToken); } RelationType relationType = await _context.RelationTypes.SingleOrDefaultAsync(x => x.Id == request.RelationTypeId, cancellationToken); _ = occurrence1 ?? throw new ArgumentNullException(nameof(occurrence1)); _ = occurrence2 ?? throw new ArgumentNullException(nameof(occurrence2)); _ = relationType ?? throw new ArgumentNullException(nameof(relationType)); var entity = new Relation(request.Name) { Description = request.Description, Occurrence1 = occurrence1, Occurrence1Id = request.Occurrence1Id, Occurrence2 = occurrence2, Occurrence2Id = request.Occurrence2Id, Occurrence1ChanceToOccurInTheRelation = request.Occurrence1ChanceToOccurInTheRelation, Occurrence2ChanceToOccurInTheRelation = request.Occurrence2ChanceToOccurInTheRelation, RelationType = relationType }; _context.Relations.Add(entity); entity.Occurrence1.RelationsAsOccurrence1.Add(entity); entity.Occurrence2.RelationsAsOccurrence2.Add(entity); await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }
public async Task <Unit> Handle(DeleteRelationCommand request, CancellationToken cancellationToken) { var entity = await _context.Relations .FindAsync(request.Id); _ = entity ?? throw new NotFoundException(nameof(Relation), request.Id); _context.Relations.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
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 <Unit> Handle(DeleteCategoryTypeCommand request, CancellationToken cancellationToken) { var entity = await _context.CategoryTypes .FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(CategoryType), request.Id); } _context.CategoryTypes.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
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); }
public async Task <int> Handle(UpsertCategoryTypeCommand request, CancellationToken cancellationToken) { CategoryType entity; if (request.Id.HasValue) { entity = await _context.CategoryTypes.FindAsync(request.Id.Value); } else { entity = new CategoryType(request.Name); _context.CategoryTypes.Add(entity); } entity.Description = request.Description; entity.Categories = request.Categories; await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }