public async Task <object> Update(CarProducentViewModel model) { var producers = await _context.CarProducents.ToListAsync(); var item = producers.Where(x => x.IdString == model.IdString.ToLower()).Where(x => x.Deleted == false).FirstOrDefault(); if (item == null) { return new { Succeeded = false, Code = "NotFound" } } ; if (model.ProducentName.Length <= 0) { return new { Succeeded = false, Code = "EmptyName" } } ; var newIdString = new string(model.ProducentName.Select(x => char.IsLetterOrDigit(x) ? x : '-').ToArray()).ToLower(); var isBusy = producers.Where(x => x.IdString == newIdString).Where(x => x.Deleted == false).FirstOrDefault(); if (isBusy != null && item.IdString != newIdString) { return new { Succeeded = false, Code = "BusyName" } } ; try { item.IdString = newIdString; item.ProducentName = model.ProducentName; var result = _context.CarProducents.Update(item); await _context.SaveChangesAsync(); return(new { Succeeded = true, }); } catch (Exception ex) { return(new { Succeeded = false, }); } }
public async Task <object> Save(CarProducentViewModel model) { try { string idString = new string(model.ProducentName.Select(x => char.IsLetterOrDigit(x) ? x : '-').ToArray()).ToLower(); var check = await _context.CarProducents .Where(x => x.Deleted == false) .Where(x => x.IdString == idString) .Include(x => x.CarModels) .FirstOrDefaultAsync(); if (check != null) { return new { Succeeded = false, Errors = new[] { new { Code = "BusyName" } } } } ; CarProducent carProducent = new CarProducent() { Id = Guid.NewGuid(), ProducentName = model.ProducentName, IdString = idString, CarModels = new List <CarModel>() }; await _context.CarProducents.AddAsync(carProducent); await _context.SaveChangesAsync(); } catch (Exception ex) { return(new { Succeeded = false }); } return(new { Succeeded = true }); }
public async Task <JsonResult> Put(string id, [FromBody] CarProducentViewModel model) { var result = await _carProducentService.Update(model); return(new JsonResult(result)); }
public async Task <JsonResult> Post([FromBody] CarProducentViewModel model) { var result = await _carProducentService.Save(model); return(new JsonResult(result)); }