public async Task <ActionResult <Machinery> > Post([FromForm] MachineryDto model) { var user = await _accountRepository.GetByIdAsync(model.User); string path = Path.Combine("img/customers", model.CustomerId.ToString(), "machinery"); string pathFull = _baseUrl.GetBaseUrl(path); var data = new Machinery { Brand = model.Brand, CategoryId = model.CategoryId, CustomerId = model.CustomerId, DateOfPurchase = model.DateOfPurchase, Model = model.Modelo, NameMachinery = model.NameMachinery, Observations = model.Observations, Serie = model.Serie, State = model.State, TechnicalSpecifications = model.TechnicalSpecifications, Ubication = model.Ubication, User = user, }; if (model.Img != null) { string nameFile = _imgService.SaveFile(model.Img, pathFull, 1280, 720); if (model.PhotoPath != null) { await _imgService.DeleteFile(pathFull, model.PhotoPath); } data.PhotoPath = nameFile; } await _genericRepository.CreateAsync(data); return(new CreatedAtRouteResult("GetMachinery", new { id = data.Id }, data)); }
public async Task <IActionResult> UpdateMachinery(int id, MachineryDto MachineryDto) { var mchnFromRepository = await _context.Machineries.FirstOrDefaultAsync(a => a.Id == id); if (mchnFromRepository == null) { return(BadRequest("Machinery not available")); } if (await _context.Machineries.AnyAsync(a => a.Name == MachineryDto.Name && a.Id != MachineryDto.Id)) { return(BadRequest("Machinery already exist")); } mchnFromRepository.Name = MachineryDto.Name; mchnFromRepository.BusinessPlaceId = MachineryDto.BusinessPlaceId; mchnFromRepository.Capacity = MachineryDto.Capacity; mchnFromRepository.PurchaseDate = DateTime.Parse(MachineryDto.PurchaseDate); mchnFromRepository.Model = MachineryDto.Model; mchnFromRepository.Value = MachineryDto.Value; if (await _context.SaveChangesAsync() > 0) { return(NoContent()); } throw new System.Exception($"Updating Machinery {id} failed on save"); }
public async Task <IActionResult> CreateMachinery(MachineryDto MachineryDto) { var mchnTOCreate = _mapper.Map <Machinery>(MachineryDto); if (await _context.Machineries.AnyAsync(a => a.Name == mchnTOCreate.Name)) { return(BadRequest("Machinery already exist")); } _context.Add(mchnTOCreate); if (await _context.SaveChangesAsync() > 0) { return(Ok()); } throw new System.Exception($"Failed to Create Machinery on save"); }
public async Task <IActionResult> Put(int id, [FromForm] MachineryDto model) { var user = await _accountRepository.GetByIdAsync(model.User); string path = Path.Combine("img/customers", model.CustomerId.ToString(), "machinery"); string pathFull = _baseUrl.GetBaseUrl(path); var data = await _genericRepository.GetAsyncById(id); data.Brand = model.Brand; data.CategoryId = model.CategoryId; data.CustomerId = model.CustomerId; data.DateOfPurchase = model.DateOfPurchase; data.Model = model.Modelo; data.NameMachinery = model.NameMachinery; data.Observations = model.Observations; data.Serie = model.Serie; data.State = model.State; data.TechnicalSpecifications = model.TechnicalSpecifications; data.Ubication = model.Ubication; data.User = user; if (model.Img != null) { string nameFile = _imgService.SaveFile(model.Img, pathFull, 1280, 720); if (model.PhotoPath != null) { await _imgService.DeleteFile(pathFull, model.PhotoPath); } data.PhotoPath = nameFile; } await _genericRepository.UpdateAsync(data); return(NoContent()); }