private async Task <ActionResult> GetViewCategoryById(long?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } var apiModel = new CategoryAPIModel(); var resp = await Get(id, response => { if (response.IsSuccessStatusCode) { var result = response.Content.ReadAsStringAsync().Result; apiModel = JsonConvert.DeserializeObject <CategoryAPIModel>(result); } }); if (apiModel.Message == "!OK") { return(HttpNotFound()); } return(View(apiModel.Result)); }
private async Task <ActionResult> GetViewById(long?id) { if (id == null) { return(new HttpStatusCodeResult(HttpStatusCode.BadRequest)); } CategoryAPIModel item = null; var resp = await GetFromAPI(response => { if (response.IsSuccessStatusCode) { var result = response.Content .ReadAsStringAsync().Result; item = JsonConvert .DeserializeObject <CategoryAPIModel>(result); } }, id.Value); if (!resp.IsSuccessStatusCode) { return(new HttpStatusCodeResult(resp.StatusCode)); } if (item.Message == "!OK" || item.Result == null) { return(HttpNotFound()); } return(View(item.Result)); }
// GET: api/Categories/5 public CategoryAPIModel Get(long?id) { var apiModel = new CategoryAPIModel(); try { if (id == null) { apiModel.Message = "!OK"; } else { apiModel.Result = service.ById(id.Value); if (apiModel.Result != null) { apiModel.Result.Products = productService .GetByCategory(id.Value).ToList(); } } } catch (System.Exception) { apiModel.Message = "!OK"; } return(apiModel); }
public IActionResult Update(int id, CategoryAPIModel cat) { using (NHibernate.ISession session = nHibernateHelper.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { var addedCat = session.Query <Category>().Single(x => x.Id == id); addedCat.Name = cat.Name; addedCat.Description = cat.Description; session.Update(addedCat); transaction.Commit(); return(NoContent()); } } }
public CategoryAPIModel Get(long id) { var apiModel = new CategoryAPIModel(); try { apiModel.Result = service.ById(id); if (apiModel.Result != null) { apiModel.Result.Products = productService.GetProductsByCategory(id).ToList(); } } catch (Exception) { apiModel.Message = "!OK"; } return(apiModel); }
public ActionResult Create(CategoryAPIModel cat) { using (NHibernate.ISession session = nHibernateHelper.OpenSession()) { using (ITransaction transaction = session.BeginTransaction()) { var addedCat = new Category { Partition = session.Query <Partition>().Single(x => x.Id == cat.PartitionId), Name = cat.Name, Description = cat.Description, }; session.SaveOrUpdate(addedCat); transaction.Commit(); var dbCat = session.Query <Category>().FirstOrDefault(x => x.Name == addedCat.Name); return(CreatedAtRoute("GetCategory", new { id = dbCat.Id }, new CategoryAPIModel(dbCat))); } } }