Exemplo n.º 1
0
        public override async Task <int> HandleCommand(AddCommand request, CancellationToken cancellationToken)
        {
            var rs = -1;

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

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

                        request.Category.Code = await storageQueries.GenarateCodeAsync(StorageKeys.CategoryCode);

                        request.Category = CreateBuild(request.Category, request.LoginSession);
                        var categoryId = await categoryRepository.Add(request.Category);

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

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

            return(rs);
        }
Exemplo n.º 2
0
        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);
        }