public HttpResponseMessage AddNewService(SERVICE service) { try { service.ISACTIVE = true; var servicesService = new ServicessService(); var id = servicesService.AddNewService(service); var response = Request.CreateResponse(HttpStatusCode.OK, id); return response; } catch (Exception e) { var error = Request.CreateResponse(HttpStatusCode.InternalServerError, e.Message); return error; } }
/// <summary> /// Add new service /// </summary> /// <param name="service"></param> /// <returns></returns> public Guid AddNewService(SERVICE service) { try { if (service == null) throw new ArgumentNullException("Service", "Service can not be null"); // check if all required fields are present if ((!service.CATEGORYID.HasValue || service.CATEGORYID.Value == Guid.Empty) || service.NAME == null || !service.PRICE.HasValue || (!service.SELLERID.HasValue || service.SELLERID.Value == Guid.Empty)) throw new ArgumentException("Some mandatory parameters required to add a new service are missing", "Service"); if (!service.ID.HasValue || service.ID.Value == Guid.Empty) service.ID = Guid.NewGuid(); service.AVGRATING = 0; service.ISACTIVE = true; service.NUMBEROFUSERS = 0; using (APIShopKaro.Models.apsteamCFHEntities db = new APIShopKaro.Models.apsteamCFHEntities()) { try { db.SERVICES.Add(service); db.SaveChanges(); } catch (System.Data.DataException e) { throw new Exception(e.InnerException.InnerException.Message); } } return service.ID.Value; } catch (Exception e) { throw; } }