示例#1
0
        private async Task <ApiResult <long> > Edit(Model.CategoryPost.Submit.Input input)
        {
            var output = new ApiResult <long>();

            var item = await _repository.CategoryGetQuery(input.Id.Value).FirstOrDefaultAsync();

            if (item == null)
            {
                output.Msgs.Add(new MsgResult("general", "common.not_exists"));
            }

            if (string.IsNullOrEmpty(input.Name))
            {
                output.Msgs.Add(new MsgResult("name", "common.name_empty"));
            }

            if (input.ParentId.HasValue == false)
            {
                output.Msgs.Add(new MsgResult("parent", "common.parent_empty"));
            }

            if (input.OrderNumber.HasValue == false)
            {
                output.Msgs.Add(new MsgResult("orderNumber", "common.order_empty"));
            }

            if (output.Msgs.Any())
            {
                return(output);
            }

            using (var transaction = await _unitOfWork.BeginTransactionAsync())
            {
                try
                {
                    var treeData = await _tree.Update <Models.ContentCategory>(new Common.Model.Tree.Update.Input
                    {
                        NewParentId        = input.ParentId.Value,
                        NewOrderNumber     = input.OrderNumber.Value,
                        CurrentParentId    = item.ParentId.Value,
                        CurrentOrderNumber = item.OrderNumber,
                        CurrentLeft        = item.Left,
                        CurrentRight       = item.Right,
                        CurrentLevel       = item.Level
                    });

                    _unitOfWork.Edit(item, nameof(item.Name), input.Name);
                    _unitOfWork.Edit(item, nameof(item.Summary), input.Summary);
                    _unitOfWork.Edit(item, nameof(item.Description), input.Description);
                    _unitOfWork.Edit(item, nameof(item.ParentId), input.ParentId);
                    _unitOfWork.Edit(item, nameof(item.Icon), input.Icon);
                    _unitOfWork.Edit(item, nameof(item.Level), treeData.Level);
                    _unitOfWork.Edit(item, nameof(item.Left), treeData.Left);
                    _unitOfWork.Edit(item, nameof(item.Right), treeData.Right);
                    _unitOfWork.Edit(item, nameof(item.OrderNumber), treeData.OrderNumber);
                    await _unitOfWork.SaveChanges();

                    if (input.FileList != null)
                    {
                        await _fileManage.Update(new File.Model.Manage.UpdateModel.Input
                        {
                            CategoryId = (long)Const.FileCategory.ContentCategory,
                            ItemId     = input.Id.ToString(),
                            IsTemp     = false,
                            FileList   = input.FileList.Select((m, index) => new File.Model.Manage.UploadModel.File {
                                Id = m.Id, OrderNumber = index
                            }).ToList(),
                        });
                    }

                    if (input.DescriptionFileList != null)
                    {
                        await _fileManage.Update(new File.Model.Manage.UpdateModel.Input
                        {
                            CategoryId = (long)Const.FileCategory.ContentCategory,
                            ItemId     = input.Id.ToString(),
                            IsTemp     = false,
                            FileList   = input.DescriptionFileList.Select((m, index) => new File.Model.Manage.UploadModel.File {
                                Id = m.Id, OrderNumber = index
                            }).ToList(),
                        });
                    }

                    transaction.Commit();
                    output.Code = Const.ApiCodeEnum.Success;
                    output.Data = item.Id;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    _logger.Error(ex, ex.Message);
                }
            }
            return(output);
        }
示例#2
0
 public async Task <ApiResult <long> > Submit(Model.CategoryPost.Submit.Input input)
 {
     return(input.Id.HasValue ? await this.Edit(input) : await this.Add(input));
 }
示例#3
0
        private async Task <ApiResult <long> > Add(Model.CategoryPost.Submit.Input input)
        {
            var output = new ApiResult <long>();

            if (string.IsNullOrEmpty(input.Name))
            {
                output.Msgs.Add(new MsgResult("name", "common.name_empty"));
            }

            if (input.ParentId.HasValue == false)
            {
                output.Msgs.Add(new MsgResult("parent", "common.parent_empty"));
            }

            if (input.OrderNumber.HasValue == false)
            {
                output.Msgs.Add(new MsgResult("orderNumber", "common.order_empty"));
            }

            if (output.Msgs.Any())
            {
                return(output);
            }

            using (var transaction = await _unitOfWork.BeginTransactionAsync())
            {
                try
                {
                    var treeData = await _tree.Insert <Models.ContentCategory>(new Common.Model.Tree.Insert.Input
                    {
                        ParentId    = input.ParentId.Value,
                        OrderNumber = input.OrderNumber.Value
                    });

                    var item = new Models.ContentCategory
                    {
                        ParentId    = input.ParentId,
                        OrderNumber = input.OrderNumber.Value,
                        Level       = treeData.Level,
                        Name        = input.Name,
                        Summary     = input.Summary,
                        Description = input.Description,
                        Icon        = input.Icon,
                        Left        = treeData.Left,
                        Right       = treeData.Right
                    };
                    await _unitOfWork._dbContext.ContentCategory.AddAsync(item);

                    await _unitOfWork.SaveChanges();

                    input.Id = item.Id;

                    if (input.FileList != null)
                    {
                        await _fileManage.Update(new File.Model.Manage.UpdateModel.Input
                        {
                            CategoryId = (long)Const.FileCategory.ContentCategory,
                            ItemId     = input.Id.ToString(),
                            IsTemp     = false,
                            FileList   = input.FileList.Select((m, index) => new File.Model.Manage.UploadModel.File {
                                Id = m.Id, OrderNumber = index
                            }).ToList(),
                        });
                    }

                    if (input.DescriptionFileList != null)
                    {
                        await _fileManage.Update(new File.Model.Manage.UpdateModel.Input
                        {
                            CategoryId = (long)Const.FileCategory.ContentCategory,
                            ItemId     = input.Id.ToString(),
                            IsTemp     = false,
                            ItemField  = "description",
                            FileList   = input.DescriptionFileList.Select((m, index) => new File.Model.Manage.UploadModel.File {
                                Id = m.Id, OrderNumber = index
                            }).ToList(),
                        });
                    }

                    transaction.Commit();
                    output.Code = Const.ApiCodeEnum.Success;
                    output.Data = item.Id;
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    _logger.Error(ex, ex.Message);
                }
            }
            return(output);
        }
示例#4
0
 public async Task <dynamic> Submit([FromBody] Model.CategoryPost.Submit.Input input)
 {
     return(await _categoryPost.Submit(input));
 }