// POST: api/Phones public HttpResponseMessage Post() { var data = HttpContext.Current.Request.Form.Get("data"); if (data == null) { return(Request.CreateResponse(HttpStatusCode.BadRequest)); } PhoneModelRequest phone = JsonConvert.DeserializeObject <PhoneModelRequest>(data); int id = _services.CreatePhone(phone); foreach (var name in HttpContext.Current.Request.Files.AllKeys) { if (!string.IsNullOrEmpty(name)) { var img = HttpContext.Current.Request.Files.Get("img"); var extension = System.IO.Path.GetExtension(img.FileName); img.SaveAs( HttpContext.Current.Server.MapPath("~/App_Data/Images/") + id + "_" + name + extension ); } } return(Request.CreateResponse(HttpStatusCode.OK)); }
public int CreatePhone(PhoneModelRequest data) { if (data != null && data.IsValidate()) { Phone phone = data.ToPhone(); using (DataLayer.DataAccess.Context context = new DataLayer.DataAccess.Context()) { context.Products.Add(phone); context.SaveChanges(); return(phone.Id); } } return(0); }
public bool UpdatePhone(int id, PhoneModelRequest data) { if (data != null && data.IsValidate()) { using (DataLayer.DataAccess.Context context = new DataLayer.DataAccess.Context()) { Phone phone = context.Products.OfType <Phone>().First(p => p.Id == id); if (phone != null) { //update data } context.SaveChanges(); return(true); } } return(false); }