public async Task <Unit> Handle(DeleteAssetCommand request, CancellationToken cancellationToken) { var entity = await _context.Assets.FindAsync(request.Id); if (entity == null) { throw new NotFoundException(nameof(Asset), request.Id); } _context.Assets.Remove(entity); await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }
public async Task <int> Handle(CreateAssetCommand request, CancellationToken cancellationToken) { Enum.TryParse(request.Department, out Department selectedDepartment); var entity = new Asset { Department = selectedDepartment, AssetName = request.AssetName, Broken = request.Broken, CountryOfDepartment = request.CountryOfDepartment, EMailAdressOfDepartment = request.EMailAdressOfDepartment, PurchaseDate = request.PurchaseDate }; await _context.Assets.AddAsync(entity); await _context.SaveChangesAsync(cancellationToken); return(entity.Id); }
public async Task <Unit> Handle(UpdateAssetCommand request, CancellationToken cancellationToken) { var entity = await _context.Assets.FirstOrDefaultAsync(c => c.Id == request.Id, cancellationToken); if (entity == null) { throw new NotFoundException(nameof(Asset), request.Id); } Enum.TryParse(request.Department, out Department selectedDepartment); entity.Department = selectedDepartment; entity.AssetName = request.AssetName; entity.Broken = request.Broken; entity.CountryOfDepartment = request.CountryOfDepartment; entity.EMailAdressOfDepartment = request.EMailAdressOfDepartment; entity.PurchaseDate = request.PurchaseDate; await _context.SaveChangesAsync(cancellationToken); return(Unit.Value); }