public LogoViewModel Add(LogoViewModel logoViewModel) { var logo = _mapper.Map <LogoViewModel, Logo>(logoViewModel); logo.Id = new Logo().Id; _logoService.Add(logo); logoViewModel = _mapper.Map <Logo, LogoViewModel>(logo); return(logoViewModel); }
public IActionResult SaveEntity(LogoViewModel logoVm) { try { if (logoVm.Id == 0) { var errorBySortOrder = "Thứ tự đã tồn tại"; if (_logoService.ValidateAddSortOrder(logoVm)) { ModelState.AddModelError("", errorBySortOrder); } if (!ModelState.IsValid) { return(BadRequest(ModelState.Select(x => x.Value.Errors).FirstOrDefault(y => y.Count > 0)?.First() .ErrorMessage)); } _logoService.Add(logoVm); } else { var errorBySortOrder = "Thứ tự đã tồn tại"; if (_logoService.ValidateUpdateSortOrder(logoVm)) { ModelState.AddModelError("", errorBySortOrder); } if (!ModelState.IsValid) { return(BadRequest(ModelState.Select(x => x.Value.Errors).FirstOrDefault(y => y.Count > 0)?.First() .ErrorMessage)); } _logoService.Update(logoVm); } //if (!ModelState.IsValid) //{ // IEnumerable<ModelError> allErrors = ModelState.Values.SelectMany(v => v.Errors); // return new BadRequestObjectResult(allErrors); //} _logoService.Save(); return(new OkObjectResult(logoVm)); } catch (Exception ex) { throw ex; } }