Esempio n. 1
0
 public async Task <APIResult> Get(int id)
 {
     return(new APIResult()
     {
         Result = 0,
         Data = await categoryQueries.GetById(id)
     });
 }
        public override async Task <int> HandleCommand(DeleteCommand request, CancellationToken cancellationToken)
        {
            var category = await categoryQueries.GetById(request.CategoryId);

            if (category == null)
            {
                throw new BusinessException("Category.NotExisted");
            }
            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        var categories = (await categoryQueries.GetAll()).Where(x => x.ParentId == category.Id).ToList();

                        for (int i = 0; i < categories.Count; i++)
                        {
                            categories[i].ParentId = null;
                            categories[i]          = UpdateBuild(categories[i], request.LoginSession);
                            await categoryRepository.Update(categories[i]);
                        }

                        category.IsDeleted = true;
                        category           = UpdateBuild(category, request.LoginSession);

                        if (await categoryRepository.Update(category) > 0)
                        {
                            rs = 0;
                        }
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch { }
                        }
                    }
                }
            }

            return(rs);
        }
        public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken)
        {
            if (request.Category == null || request.Category.Id == 0)
            {
                throw new BusinessException("Category.NotExisted");
            }

            var category = await categoryQueries.GetById(request.Category.Id);

            if (category == null)
            {
                throw new BusinessException("Category.NotExisted");
            }

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        request.Category.CreatedDate = category.CreatedDate;
                        request.Category.CreatedBy   = category.CreatedBy;
                        request.Category             = UpdateBuild(request.Category, request.LoginSession);
                        //request.Category.Code = category.Code;
                        rs = await categoryRepository.Update(request.Category);

                        if (rs == 0)
                        {
                            return(-1);
                        }

                        //for language
                        // languages
                        foreach (var item in request.Category.Languages)
                        {
                            item.CategoryId = request.Category.Id;
                            await categoryRepository.AddOrUpdateLanguage(item);
                        }

                        rs = 0;
                    }
                    catch (Exception ex)
                    {
                        throw ex;
                    }
                    finally
                    {
                        if (rs == 0)
                        {
                            trans.Commit();
                        }
                        else
                        {
                            try
                            {
                                trans.Rollback();
                            }
                            catch { }
                        }
                    }
                }
            }

            return(rs);
        }