public HttpResponseMessage DodajKategorie(Models.ModeleAPI.DodawanieKategoriiModel model) { if (WroBL.DAL.DatabaseUtils.ExistsElement("select first 1 1 from categories c where c.name='" + model.Nazwa + "'")) { return Request.CreateResponse(HttpStatusCode.OK, "Category is already exists"); } else { WroBL.DAL.DatabaseUtils.DatabaseCommand("insert into categories (name,img_link) values('" + model.Nazwa + "','img')"); JednaKategoriaModel toReturn = new JednaKategoriaModel() { Id = Int32.Parse(WroBL.DAL.DatabaseUtils.GetOneElement("select id from categories c where c.name='" + model.Nazwa + "'")), LinkDoObrazka = "img", Nazwa = model.Nazwa }; return Request.CreateResponse(HttpStatusCode.OK, toReturn); } }
public JsonResult Upload(FormCollection form) { HttpPostedFileBase file; try { file = Request.Files[0]; } catch (ArgumentOutOfRangeException) { return Json("ERRORBrak obrazka", JsonRequestBehavior.AllowGet); } string fileName = file.FileName; string mimeType = file.ContentType; System.IO.Stream fileContent = file.InputStream; try { string savePath = Server.MapPath(@"/files/") + fileName; file.SaveAs(savePath); } catch (System.IO.IOException ex) { return Json("ERRORProblem z dodaniem obrazka", JsonRequestBehavior.AllowGet); } var nazwaKategorii = form["nazwa_kategorii"]; if( String.IsNullOrWhiteSpace(nazwaKategorii)) return Json("ERRORBrak nazwy", JsonRequestBehavior.AllowGet); if (WroBL.DAL.DatabaseUtils.ExistsElement("select first 1 1 from categories c where c.name='" + nazwaKategorii + "'")) { return Json("ERRORKategoria już istnieje", JsonRequestBehavior.AllowGet); } else { WroBL.DAL.DatabaseUtils.DatabaseCommand("insert into categories (name,img_link) values('" + nazwaKategorii + "','" + "/files/"+ fileName + "')"); JednaKategoriaModel toReturn = new JednaKategoriaModel() { Id = Int32.Parse(WroBL.DAL.DatabaseUtils.GetOneElement("select id from categories c where c.name='" + nazwaKategorii + "'")), LinkDoObrazka = "/files/" + fileName, Nazwa = nazwaKategorii }; return Json(toReturn, JsonRequestBehavior.AllowGet); //return Json("OK", JsonRequestBehavior.AllowGet); } }