Exemplo n.º 1
0
 public ActionResult addCat(Category category)
 {
     db.Category.Add(category);
     db.SaveChanges();
     return RedirectToAction("changeCategories");
 }
Exemplo n.º 2
0
 public ActionResult changeCat(Category category)
 {
     return RedirectToAction("changeCategories");
 }
        //POST api/categories
        public HttpResponseMessage PostCategory([FromBody]CategoryModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))]string sessionKey)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions<HttpResponseMessage>(
              () =>
              {
                  using (var context = new StoreContext())
                  {
                      this.ValidateSessionKey(sessionKey);

                      var admin = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey);
                      if (admin == null)
                      {
                          throw new ArgumentException("Invalid SessionKey or user is already logouted");
                      }
                      else if (admin.IsAdmin != true)
                      {
                          throw new ArgumentException("Unauthorized Access");
                      }

                      //TODO: Validate Escaping

                      var name = model.Name.ToLower();

                      var existingCategory = context.Categories.FirstOrDefault(c => c.Name.ToLower() == name);

                      if (existingCategory != null)
                      {
                          throw new ArgumentException("Category with that name already exists!");
                      }

                      var category = new Category
                        {
                            Name = model.Name,
                            Description = model.Description,
                            ImageSource = model.ImageSource
                        };

                      context.Categories.Add(category);
                      context.SaveChanges();
                  }

                  var response = new HttpResponseMessage(HttpStatusCode.Created);
                  return response;
              });

            return responseMsg;
        }