Exemplo n.º 1
0
        public async Task <TResponse <bool> > Add(int userId,
                                                  InsertUserModel request,
                                                  int permissionId,
                                                  string path)
        {
            try
            {
                var checkValid = await CheckPermission(userId,
                                                       permissionId);

                if (checkValid.IsSuccess)
                {
                    var canInsert = await CanInsert(request);

                    if (canInsert.IsSuccess)
                    {
                        string avatar = string.Empty;
                        var    image  = request.Avatar;
                        if (image != null &&
                            image.Length > 0)
                        {
                            avatar = $"{request.Username}.png";
                        }

                        var result = await WriteRepository.ExecuteScalarAsync <int>(SqlQuery.USER_INSERT,
                                                                                    new
                        {
                            request.Username,
                            Password = Sha512(request.Password),
                            request.DisplayName,
                            request.FullName,
                            request.PhoneNumber,
                            request.Email,
                            request.BranchId,
                            request.RoleId,
                            Avatar      = avatar,
                            UserCreated = userId,
                            DateCreated = DateTime.Now,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

                        if (result.IsSuccess)
                        {
                            if (result.Data > 0)
                            {
                                if (image != null &&
                                    image.Length > 0)
                                {
                                    try
                                    {
                                        if (!Directory.Exists(path))
                                        {
                                            Directory.CreateDirectory(path);
                                        }

                                        using (FileStream fileStream = File.Create(Path.Combine(path,
                                                                                                request.Username) + ".png"))
                                        {
                                            image.CopyTo(fileStream);
                                            fileStream.Flush();
                                        }
                                    }
                                    catch (Exception exception)
                                    {
                                        Log(exception);
                                    }
                                }

                                #region Update redis cache

                                await _userCacheService.AddOrUpdate(new UserCacheModel
                                {
                                    Id          = result.Data,
                                    Username    = request.Username,
                                    DisplayName = request.DisplayName,
                                    Email       = request.Email,
                                    PhoneNumber = request.PhoneNumber,
                                    FullName    = request.FullName
                                });

                                #endregion

                                return(await Ok(true));
                            }

                            return(await Fail <bool>(ErrorEnum.BAD_REQUEST.GetStringValue()));
                        }

                        return(await Fail <bool>(result.Message));
                    }

                    return(await Fail <bool>(canInsert.Message));
                }

                return(await Fail <bool>(checkValid.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }