Ejemplo n.º 1
0
 public ActionResult Save(CategoryFormModel form)
 {
     if(ModelState.IsValid)
     {
         var command = new CreateOrUpdateCategoryCommand(form.CategoryId,form.Name, form.Description);
        IEnumerable<ValidationResult> errors=  commandBus.Validate(command);
         ModelState.AddModelErrors(errors);              
        if (ModelState.IsValid)
        {
            var result = commandBus.Submit(command);
            if (result.Success)
            {
                //updating data to Cache
                var categories = categoryRepository.GetAll();
                cache.Put("categories", categories);
                return RedirectToAction("Index");
            }
        }                
     }   
     //if fail
     if (form.CategoryId == 0)
         return View("Create",form);
     else
         return View("Edit", form);
 }         
Ejemplo n.º 2
0
 // PUT /api/categories/5
 public HttpResponseMessage Put(int id, CategoryModel category)
 {
     if (ModelState.IsValid)
     {
         var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description);
         var result = commandBus.Submit(command);
         return new HttpResponseMessage(HttpStatusCode.NoContent);
     }
     throw new HttpResponseException(HttpStatusCode.BadRequest);
 }
Ejemplo n.º 3
0
 // POST /api/categories
 public HttpResponseMessage<CategoryWithExpense> Post(CategoryModel category)
 {
                                 
         if (ModelState.IsValid)
         {
             var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description);   
             var result = commandBus.Submit(command);
             if (result.Success)
             {
                 var categoryWithExpense = new CategoryWithExpense { CategoryName = category.CategoryName, Description = category.Description, TotalExpenses = 0 };
                 var response = new HttpResponseMessage<CategoryWithExpense>(categoryWithExpense, HttpStatusCode.Created);
                 string uri = Url.Route(null, new { id = category.CategoryId });
                 response.Headers.Location = new Uri(Request.RequestUri, uri);
                 return response;
             }
         }    
      throw new HttpResponseException(HttpStatusCode.BadRequest);
 }
Ejemplo n.º 4
0
 // POST /api/category
 public HttpResponseMessage Post(CategoryModel category)
 {
     if (ModelState.IsValid)
     {
     var command = new CreateOrUpdateCategoryCommand(category.CategoryId, category.CategoryName, category.Description);
     var result = commandBus.Submit(command);
     if (result.Success)
     {
         var response = Request.CreateResponse(HttpStatusCode.Created, category);
         string uri = Url.Link("DefaultApi", new { id = category.CategoryId });
         response.Headers.Location = new Uri(uri);
         return response;
     }
     }
     else
     {
     return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
     }
     throw new HttpResponseException(HttpStatusCode.BadRequest);
 }