public ActionResult Edit(CategoryModel model)
        {
            if(ModelState.IsValid)
            {
                long id = _service.SaveCategory(model);

                return RedirectToAction("view", "category", new
                {
                    id = id
                });
            }

            EditCategoryViewModel view = new EditCategoryViewModel
            {
                Category = model
            };

            return View(view);
        }
예제 #2
0
        public long SaveCategory(CategoryModel model)
        {
            CategoryRepository cRepo = new CategoryRepository(_dal);

            Category category = new Category
            {
                id = model.Id,
                name = model.Name.Escape().Trim()
            };

            if(category.id == 0)
            {
                try
                {
                    _dal.BeginTransaction();

                    long ret = cRepo.Insert(category);
                    if (ret == 0)
                        throw new Exception("Category was not inserted.");

                    long id = cRepo.GetLastInsertId();

                    _dal.CommitTransaction();

                    return id;
                }
                catch(Exception ex)
                {
                    _dal.RollbackTransaction();
                    log.Error(ex);
                    throw;
                }
            }
            else
            {
                try
                {
                    _dal.BeginTransaction();

                    long ret = cRepo.Update(category);
                    if (ret == 0)
                        throw new Exception("Category was not updated.");

                    _dal.CommitTransaction();

                    return model.Id;
                }
                catch(Exception ex)
                {
                    _dal.RollbackTransaction();
                    log.Error(ex);
                    throw;
                }
            }
        }