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

            try
            {
                if (string.IsNullOrEmpty(input.UserName) || input.UserName.Length < 6 || input.UserName.Length > 20)
                {
                    output.Msgs.Add(new MsgResult("userName", "register.userName_invalid"));
                }
                else if (await _repository.GetQuery(new Model.Repository.GetQuery.Input {
                    UserName = input.UserName
                }).AnyAsync())
                {
                    output.Msgs.Add(new MsgResult("userName", "register.userName_taken"));
                }

                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
                }).AnyAsync())
                {
                    output.Msgs.Add(new MsgResult("email", "register.email_taken"));
                }

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

                if (string.IsNullOrEmpty(input.PhoneNumber))
                {
                    output.Msgs.Add(new MsgResult("phoneNumber", "register.phoneNumber_empty"));
                }

                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 = new Models.User
                            {
                                Id           = _repository.NewUserId(),
                                UserName     = input.UserName,
                                Email        = input.Email,
                                FullName     = input.FullName,
                                PhoneNumber  = input.PhoneNumber,
                                PasswordHash = Utility.PasswordEncrypt(input.Password),
                                DateCreated  = _current.Now,
                                IsActivated  = true,
                                IsBanned     = false,
                                UserByRole   = new List <Models.UserByRole> {
                                    new Models.UserByRole
                                    {
                                        RoleId = Const.Role.User,
                                    }
                                },
                            };
                            await _unitOfWork._dbContext.User.AddAsync(user);

                            await _unitOfWork.SaveChanges();

                            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);
        }
示例#2
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);
        }
示例#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);
        }