Exemple #1
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);
        }
Exemple #2
0
        public async Task <ApiResult <long> > Submit(Model.BrandPost.Submit.Input input)
        {
            var output = new ApiResult <long>();

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

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

            var name = input.Name.Trim();
            var queryBrandWithName = _repository.ProductBrandGetQuery(new Model.Repository.ProductBrandGetQuery.Input
            {
                Name = name
            });

            try
            {
                if (input.Id.HasValue)
                {
                    var item = await _repository.ProductBrandGetQuery(input.Id.Value).FirstOrDefaultAsync();

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

                    if (await queryBrandWithName.Where(x => !x.Name.Equals(item.Name)).AnyAsync())
                    {
                        output.Msgs.Add(new MsgResult("name", "common.name_dupplicate"));
                        return(output);
                    }

                    _unitOfWork.Edit(item, nameof(item.Name), name);
                    _unitOfWork.Edit(item, nameof(item.PopularOrderNumber), input.PopularOrderNumber);
                    await _unitOfWork.SaveChanges();

                    output.Code = Const.ApiCodeEnum.Success;
                    output.Data = item.Id;
                }
                else
                {
                    if (await queryBrandWithName.AnyAsync())
                    {
                        output.Msgs.Add(new MsgResult("name", "common.name_dupplicate"));
                        return(output);
                    }

                    var item = new Models.ProductBrand
                    {
                        Name = input.Name,
                        PopularOrderNumber = input.PopularOrderNumber,
                    };
                    await _unitOfWork._dbContext.ProductBrand.AddAsync(item);

                    await _unitOfWork.SaveChanges();

                    input.Id = item.Id;

                    output.Code = Const.ApiCodeEnum.Success;
                    output.Data = item.Id;
                }

                if (input.FileList != null)
                {
                    await _fileManage.Update(new File.Model.Manage.UpdateModel.Input
                    {
                        CategoryId = (long)Const.FileCategory.Brand,
                        ItemId     = input.Id.ToString(),
                        IsTemp     = false,
                        FileList   = input.FileList.Select((m, index) => new File.Model.Manage.UploadModel.File {
                            Id = m.Id, OrderNumber = index
                        }).ToList(),
                    });
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, ex.Message);
            }
            return(output);
        }
Exemple #3
0
        public async Task <dynamic> Submit(Model.Profile.Submit.Input input)
        {
            var output = new ApiResult <long>();

            try
            {
                var currentUserId = _current.UserId;
                var isAdmin       = await _repository.IsAdmin(currentUserId) || string.IsNullOrEmpty(_current.LoginId) == false;

                if (string.IsNullOrEmpty(input.FullName))
                {
                    output.Msgs.Add(new MsgResult("fullName", "register.fullName_empty"));
                }

                if (string.IsNullOrEmpty(input.Email) || Utility.EmailCheck(input.Email) == false)
                {
                    output.Msgs.Add(new MsgResult("email", "register.email_invalid"));
                }
                else if (await _repository.GetQuery(new Model.Repository.GetQuery.Input {
                    Email = input.Email
                }).Where(m => m.Id != currentUserId).AnyAsync())
                {
                    output.Msgs.Add(new MsgResult("email", "register.email_taken"));
                }

                if (string.IsNullOrEmpty(input.Password) == false || string.IsNullOrEmpty(input.ConfirmPassword) == false)
                {
                    if (Utility.PasswordCheckSecurity(input.Password) == false)
                    {
                        output.Msgs.Add(new MsgResult("password", "register.password_invalid"));
                    }
                    else if (input.Password != input.ConfirmPassword)
                    {
                        output.Msgs.Add(new MsgResult("confirmPassword", "register.confirmPassword_incorrect"));
                    }
                }

                if (output.Msgs.Any() == false)
                {
                    using (var transaction = await _unitOfWork.BeginTransactionAsync())
                    {
                        try
                        {
                            var user = await _repository.GetQuery(currentUserId).FirstOrDefaultAsync();

                            _unitOfWork.Edit(user, nameof(user.FullName), input.FullName);
                            _unitOfWork.Edit(user, nameof(user.PhoneNumber), input.PhoneNumber);
                            _unitOfWork.Edit(user, nameof(user.Email), input.Email);
                            if (string.IsNullOrEmpty(input.Password) == false)
                            {
                                _unitOfWork.Edit(user, nameof(user.PasswordHash), Utility.PasswordEncrypt(input.Password));
                            }
                            _unitOfWork.Edit(user, nameof(user.Email), input.Email);

                            await _unitOfWork.SaveChanges();

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

                            transaction.Commit();
                            output.Code = Const.ApiCodeEnum.Success;
                        }
                        catch (Exception ex)
                        {
                            transaction.Rollback();
                            _logger.Error(ex, ex.Message);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                _logger.Error(ex, ex.Message);
            }

            return(output);
        }
Exemple #4
0
        public async Task <ApiResult <long> > Submit(Model.Post.Submit.Input input)
        {
            var currentUserId = _current.UserId;

            var output = new ApiResult <long>();

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

            if (input.CategoryId.HasValue == false)
            {
                output.Msgs.Add(new MsgResult("category", "common.category_empty"));
            }

            switch (input.PriceType)
            {
            case "normal":
                input.PriceSource    = null;
                input.SaleOffPercent = null;
                break;

            case "saleoff":
                if (input.PriceSource.HasValue == false || input.PriceSource.Value <= 0)
                {
                    output.Msgs.Add(new MsgResult("priceSource", "common.value_empty"));
                }
                else
                if (input.PriceSaleOff.HasValue == false || input.PriceSaleOff.Value <= 0)
                {
                    output.Msgs.Add(new MsgResult("priceSaleOff", "common.value_empty"));
                }
                else
                {
                    input.Price = input.PriceSaleOff;
                }
                break;
            }

            if (output.Msgs.Any() == false)
            {
                try
                {
                    if (input.Id.HasValue)
                    {
                        var item = await _repository.GetQuery(new Model.Repository.GetQuery.Input
                        {
                            Id     = input.Id.Value,
                            UserId = await _userRepository.IsAdmin(currentUserId) ? null : currentUserId,
                        }).FirstOrDefaultAsync();

                        if (item != null)
                        {
                            _unitOfWork._dbContext.ProductByCategory.RemoveRange(_unitOfWork._dbContext.ProductByCategory.Where(m => m.ItemId == item.Id));
                            await _unitOfWork._dbContext.ProductByCategory.AddAsync(new Models.ProductByCategory {
                                CategoryId = input.CategoryId.Value, ItemId = item.Id
                            });

                            _unitOfWork.Edit(item, nameof(item.Name), input.Name);
                            _unitOfWork.Edit(item, nameof(item.Price), input.Price);
                            _unitOfWork.Edit(item, nameof(item.PriceSource), input.PriceSource);
                            _unitOfWork.Edit(item, nameof(item.SaleOffPercent), input.SaleOffPercent);

                            _unitOfWork.Edit(item, nameof(item.IsHidden), input.IsHidden);
                            _unitOfWork.Edit(item, nameof(item.AllowOrder), input.AllowOrder);
                            _unitOfWork.Edit(item, nameof(item.Summary), input.Summary);
                            _unitOfWork.Edit(item, nameof(item.Description), input.Description);
                            _unitOfWork.Edit(item, nameof(item.Quantity), input.Quantity);
                            _unitOfWork.Edit(item, nameof(item.BrandId), input.BrandId);
                            await _unitOfWork.SaveChanges();

                            output.Code = Const.ApiCodeEnum.Success;
                            output.Data = item.Id;
                        }
                        else
                        {
                            output.Msgs.Add(new MsgResult("general", "common.not_exists"));
                        }
                    }
                    else
                    {
                        var item = new Models.Product
                        {
                            UserId         = currentUserId,
                            Name           = input.Name,
                            Price          = input.Price,
                            PriceSource    = input.PriceSource,
                            SaleOffPercent = input.SaleOffPercent,

                            IsHidden          = input.IsHidden,
                            AllowOrder        = input.AllowOrder,
                            Summary           = input.Summary,
                            Description       = input.Description,
                            Quantity          = input.Quantity,
                            BrandId           = input.BrandId,
                            DateCreated       = _current.Now,
                            ProductByCategory = new List <Models.ProductByCategory> {
                                new Models.ProductByCategory {
                                    CategoryId = input.CategoryId.Value
                                }
                            },
                        };
                        await _unitOfWork._dbContext.Product.AddAsync(item);

                        await _unitOfWork.SaveChanges();

                        input.Id = item.Id;

                        output.Code = Const.ApiCodeEnum.Success;
                        output.Data = item.Id;
                    }

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

                        await _fileManage.Update(new File.Model.Manage.UpdateModel.Input
                        {
                            CategoryId = (long)Const.FileCategory.Product,
                            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(),
                        });
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, ex.Message);
                }
            }

            return(output);
        }
Exemple #5
0
        public async Task <ApiResult <long> > Submit(Model.Post.Submit.Input input)
        {
            var userId = _current.UserId;

            var output = new ApiResult <long>();

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

            if (input.CategoryId.HasValue == false)
            {
                output.Msgs.Add(new MsgResult("category", "common.category_empty"));
            }

            if (output.Msgs.Any() == false)
            {
                try
                {
                    if (input.Id.HasValue)
                    {
                        var item = await _repository.GetQuery(input.Id.Value).FirstOrDefaultAsync();

                        if (item != null)
                        {
                            _unitOfWork._dbContext.ContentByCategory.RemoveRange(_unitOfWork._dbContext.ContentByCategory.Where(m => m.ItemId == item.Id));
                            await _unitOfWork._dbContext.ContentByCategory.AddAsync(new Models.ContentByCategory {
                                CategoryId = input.CategoryId.Value, ItemId = item.Id
                            });

                            _unitOfWork.Edit(item, nameof(item.Name), input.Name);
                            _unitOfWork.Edit(item, nameof(item.Summary), input.Summary);
                            _unitOfWork.Edit(item, nameof(item.IsHidden), input.IsHidden);
                            _unitOfWork.Edit(item, nameof(item.DateCreated), input.DateCreated);
                            _unitOfWork.Edit(item, nameof(item.Description), input.Description);
                            _unitOfWork.Edit(item, nameof(item.Link), input.Link);
                            await _unitOfWork.SaveChanges();

                            output.Code = Const.ApiCodeEnum.Success;
                            output.Data = item.Id;
                        }
                        else
                        {
                            output.Msgs.Add(new MsgResult("general", "common.not_exists"));
                        }
                    }
                    else
                    {
                        var item = new Models.Content
                        {
                            UserId            = userId,
                            Name              = input.Name,
                            Summary           = input.Summary,
                            IsHidden          = input.IsHidden,
                            Description       = input.Description,
                            Link              = input.Link,
                            DateCreated       = input.DateCreated ?? _current.Now,
                            ContentByCategory = new List <Models.ContentByCategory> {
                                new Models.ContentByCategory {
                                    CategoryId = input.CategoryId.Value
                                }
                            },
                        };
                        await _unitOfWork._dbContext.Content.AddAsync(item);

                        await _unitOfWork.SaveChanges();

                        input.Id = item.Id;

                        output.Code = Const.ApiCodeEnum.Success;
                        output.Data = item.Id;
                    }

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

                    await _fileManage.Update(new File.Model.Manage.UpdateModel.Input
                    {
                        CategoryId = (long)Const.FileCategory.Content,
                        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(),
                    });
                }
                catch (Exception ex)
                {
                    _logger.Error(ex, ex.Message);
                }
            }

            return(output);
        }