public async Task <IActionResult> Create(Service service) { if (ModelState.IsValid) { try { service.IsActive = true; await _serviceRepository.CreateAsync(service); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { if (ex.InnerException.Message.Contains("duplicate")) { if (ModelState.IsValid) { ModelState.AddModelError(string.Empty, $"There is allready a Service registered with the name {service.ServiceType} please insert another"); } } else { ModelState.AddModelError(string.Empty, ex.InnerException.Message); } } } return(View(service)); }
public async Task <StatusCodeResult> Create([FromBody] ServiceViewModel viewModel) { if (await _service.CreateAsync(viewModel.ToModel()) == 1) { return(StatusCode((int)HttpStatusCode.OK)); } else { return(StatusCode((int)HttpStatusCode.ExpectationFailed)); } }
public IActionResult Create(Product cat) { if (ModelState.IsValid) { var res = prdRepository.CreateAsync(cat).Result; // redirect to Index action of the current controller return(RedirectToAction("Index")); } ViewBag.CategoryRowId = catRepository.GetAsync().Result.ToList(); // else stay on the same view and show error messages return(View(cat)); }
public IActionResult Post(Category cat) { if (ModelState.IsValid) { if (cat.BasePrice < 0) { throw new Exception("Price cannot be -Ve"); } var res = repository.CreateAsync(cat).Result; return(Ok(res)); } return(BadRequest(cat)); }
public IActionResult Create(Category cat) { try { // check if the category is valid as per // data annotation rules if (ModelState.IsValid) { var cats = catRepository.GetAsync().Result.ToList(); var ct = cats.Where(c => c.CategoryId == cat.CategoryId).FirstOrDefault(); if (ct != null) { throw new Exception("Category Id is already Present"); } //foreach (var item in cats) //{ // if (item.CategoryId.Trim() == cat.CategoryId.Trim()) // { // throw new Exception("Category Id is already Present"); // } //} var res = catRepository.CreateAsync(cat).Result; // redirect to Index action of the current controller return(RedirectToAction("Index")); } // else stay on the same view and show error messages return(View(cat)); } catch (Exception ex) { // return the Error Page with details //return View("Error", new ErrorViewModel() //{ // ErrorMessage = ex.Message, // ControllerName = this.RouteData.Values["controller"].ToString(), // ActionName = this.RouteData.Values["action"].ToString() //}); throw ex; } }
public async Task <ActionResult <Service> > CreateWorkshopService([FromRoute] int workshopId, [FromBody] ServiceCreateDto serviceDto) { if (!ModelState.IsValid) { return(BadRequest(serviceDto)); } Workshop workshop = await _workshopRepo.GetByIdAsync(workshopId); if (workshop == null) { return(NotFound(new { error = $"workshop with id {workshopId} could not be found" })); } else { serviceDto.WorkshopID = workshopId; Service service = _mapper.Map <Service>(serviceDto); int createdServiceId = await _serviceRepo.CreateAsync(service); await _workshopRepo.UpdateAsync(workshop); return(CreatedAtAction(nameof(GetSingleWorkshopService), new { workshopId = workshopId, serviceId = createdServiceId }, serviceDto)); } }