public static void Remove(this CarMaterialStock carMaterialStock, InUpdateMaterialAssignmentDTO materialAssignmentDTO) { carMaterialStock.Amount -= materialAssignmentDTO.Amount; carMaterialStock.CreatedAt = DateTimeOffset .FromUnixTimeSeconds(materialAssignmentDTO.CreatedAt).UtcDateTime; }
public static void Add(this ICollection <CarMaterialStock> carMaterialStocks, InUpdateMaterialAssignmentDTO materialAssignmentDTO) { carMaterialStocks.Add(new CarMaterialStock() { Amount = materialAssignmentDTO.Amount, CreatedAt = DateTimeOffset .FromUnixTimeSeconds(materialAssignmentDTO.CreatedAt).UtcDateTime, MaterialId = materialAssignmentDTO.MaterialId, }); }
public async Task <IActionResult> AssignMaterialCarAsync([FromRoute] int id, [FromBody] InUpdateMaterialAssignmentDTO materialAssignmentDTO) { var userId = HttpContext.User.GetUserId(); var car = await _repositoryManager.CarRepository.GetAsync(id); var material = await _repositoryManager.MaterialRepository .GetAsync(materialAssignmentDTO.MaterialId); if (car == null || material == null) { return(NotFound()); } if (material.Amount < materialAssignmentDTO.Amount) { return(BadRequest()); } if (!car.Materials.Any(cms => cms.CarId == id && cms.MaterialId == materialAssignmentDTO.MaterialId)) { car.Materials.Add(materialAssignmentDTO); } else { car.Materials.First(cms => cms.CarId == id && cms.MaterialId == materialAssignmentDTO.MaterialId) .Update(materialAssignmentDTO); } material.RemoveAmount(userId.Value, materialAssignmentDTO.Amount, id); _repositoryManager.CarRepository.Update(car); await _repositoryManager.SaveAsync(); return(Ok()); }
public async Task <IActionResult> UnassignMaterialCarAsync([FromRoute] int id, [FromBody] InUpdateMaterialAssignmentDTO materialAssignmentDTO) { var userId = HttpContext.User.GetUserId(); if (!userId.HasValue) { return(Unauthorized()); } var car = await _repositoryManager.CarRepository.GetAsync(id); var material = await _repositoryManager.MaterialRepository .GetAsync(materialAssignmentDTO.MaterialId); var carMaterialStock = car?.Materials .FirstOrDefault(cms => cms.CarId == id && cms.MaterialId == materialAssignmentDTO.MaterialId); if (car == null || material == null || carMaterialStock == null) { return(NotFound()); } if (carMaterialStock.Amount < materialAssignmentDTO.Amount) { return(BadRequest()); } carMaterialStock.Remove(materialAssignmentDTO); material.AddAmount(userId.Value, materialAssignmentDTO.Amount, id); _repositoryManager.CarRepository.Update(car); await _repositoryManager.SaveAsync(); return(Ok()); }