Пример #1
0
 public async Task <APIResult> GetCatetory(int id)
 {
     return(new APIResult()
     {
         Result = 0,
         Data = await pesticideCategoryQueries.GetById(id)
     });
 }
Пример #2
0
        public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken)
        {
            if ((request.Pesticide?.CategoryId ?? 0) > 0)
            {
                var category = await pesticideCategoryQueries.GetById(request.Pesticide.CategoryId ?? 0);

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

            var rs = -1;

            using (var conn = DALHelper.GetConnection())
            {
                conn.Open();
                using (var trans = conn.BeginTransaction())
                {
                    try
                    {
                        pesticideRepository.JoinTransaction(conn, trans);
                        pesticideQueries.JoinTransaction(conn, trans);

                        if (request.Pesticide == null)
                        {
                            throw new BusinessException("AddWrongInformation");
                        }

                        request.Pesticide.Code = await storageQueries.GenarateCodeAsync(StorageKeys.PesticideCode);

                        request.Pesticide = CreateBuild(request.Pesticide, request.LoginSession);
                        var pesticideId = await pesticideRepository.Add(request.Pesticide);

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

            return(rs);
        }
Пример #3
0
        public override async Task <int> HandleCommand(DeleteCommand request, CancellationToken cancellationToken)
        {
            var pesticideCategory = await pesticideCategoryQueries.GetById(request.Id);

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

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

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

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

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

            return(rs);
        }
Пример #4
0
        public override async Task <int> HandleCommand(UpdateCommand request, CancellationToken cancellationToken)
        {
            if (request.Pesticide == null || request.Pesticide.Id == 0)
            {
                throw new BusinessException("Pesticide.NotExisted");
            }

            var pesticide = await pesticideQueries.GetById(request.Pesticide.Id);

            if (pesticide == null)
            {
                throw new BusinessException("Pesticide.NotExisted");
            }

            if ((request.Pesticide?.CategoryId ?? 0) > 0)
            {
                var category = await pesticideCategoryQueries.GetById(request.Pesticide.CategoryId ?? 0);

                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.Pesticide.CreatedDate = pesticide.CreatedDate;
                        request.Pesticide.CreatedBy   = pesticide.CreatedBy;
                        request.Pesticide             = UpdateBuild(request.Pesticide, request.LoginSession);
                        request.Pesticide.Code        = string.IsNullOrWhiteSpace(pesticide.Code)
                            ? (await storageQueries.GenarateCodeAsync(StorageKeys.PesticideCode))
                            : pesticide.Code;

                        rs = await pesticideRepository.Update(request.Pesticide);

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

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

            return(rs);
        }