public HttpResponseMessage Post(HttpRequestMessage request, WarrantyViewModel warrantyVm) { return(CreateHttpResponse(request, () => { HttpResponseMessage response = null; if (!ModelState.IsValid) { request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } else { Warranty newWarranty = new Warranty(); newWarranty.UpdateWarranty(warrantyVm); var category = _warrantyService.Add(newWarranty); _warrantyService.SaveChanges(); response = request.CreateResponse(HttpStatusCode.Created, category); } return response; })); }
public ActionResult Create(WarrantyViewModel viewModel) { try { int endYear = Convert.ToInt32(viewModel.EndDate.Year.ToString()); int warrantyYear = Convert.ToInt32(viewModel.DateWarranty.Year.ToString()); if (String.IsNullOrEmpty(viewModel.Reason) || String.IsNullOrEmpty(viewModel.Result) || endYear < 1970 || warrantyYear < 1970) { return(View(viewModel)); } DateTime endD = Convert.ToDateTime(viewModel.EndDate.ToString()); DateTime warrantyD = Convert.ToDateTime(viewModel.DateWarranty.ToString()); int compareDate = DateTime.Compare(endD, warrantyD); if (compareDate <= 0) { ModelState.AddModelError("EndDate", "EndDate must have a date greater than DateWarranty"); return(View(viewModel)); } else { var warranty = new Warranty() { Reason = viewModel.Reason.Trim(), EndDate = viewModel.EndDate, DateWarranty = viewModel.DateWarranty, Result = viewModel.Result.Trim(), AssetID = viewModel.Asset.ID, Active = true }; _warrantyService.Add(warranty); _warrantyService.SaveChanges(); SetAlert("Add Warranty success", "success"); } } catch (Exception e) { SetAlert("Add Warranty error", "error"); } return(RedirectToAction("Index")); }