Exemplo n.º 1
0
        /// <summary>
        ///     Delete Site
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="siteId"></param>
        /// <returns></returns>
        public async Task <TResponse <bool> > Delete(int userId,
                                                     int siteId)
        {
            try
            {
                var canDelete = await CanDelete(siteId);

                if (canDelete.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.SITE_DELETE, new
                    {
                        Id          = siteId,
                        UserUpdated = userId
                    });

                    if (result.IsSuccess)
                    {
                        if (result.Data == 0)
                        {
                            return(await Fail <bool>(string.Format(ErrorEnum.SqlQueryCanNotExecute.GetStringValue(), "SITE_DELETE")));
                        }

                        return(await Ok(true));
                    }

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

                return(await Fail <bool>(canDelete.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 2
0
        public async Task <TResponse <bool> > RemoveByKey(int userId,
                                                          string key)
        {
            try
            {
                var result = await WriteRepository.ExecuteAsync(SqlQuery.PERSISTED_GRANT_REMOVE_BY_KEY, new
                {
                    UserUpdated = userId,
                    Key         = key
                });

                if (result.IsSuccess)
                {
                    if (result.Data > 0)
                    {
                        return(await Ok(true));
                    }

                    return(await Fail <bool>(string.Format(ErrorEnum.SqlQueryCanNotExecute.GetStringValue(), "PERSISTED_GRANT_REMOVE_BY_KEY")));
                }

                return(await Fail <bool>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 3
0
        public async Task <TResponse <bool> > RemoveBySubjectIdClientIdType(int userId,
                                                                            string subjectId,
                                                                            string clientId,
                                                                            string type)
        {
            try
            {
                var result = await WriteRepository.ExecuteAsync(SqlQuery.PERSISTED_GRANT_REMOVE_BY_SUBJECT_CLIENT_TYPE, new
                {
                    UserUpdated = userId,
                    SubjectId   = subjectId,
                    ClientId    = clientId,
                    Type        = type
                });

                if (result.IsSuccess)
                {
                    if (result.Data > 0)
                    {
                        return(await Ok(true));
                    }

                    return(await Fail <bool>(string.Format(ErrorEnum.SqlQueryCanNotExecute.GetStringValue(), "PERSISTED_GRANT_REMOVE_BY_SUBJECT_CLIENT_TYPE")));
                }

                return(await Fail <bool>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 4
0
        private async Task <TResponse <bool> > Update(int userId,
                                                      PersistedGrantDto persistedGrant)
        {
            try
            {
                var result = await WriteRepository.ExecuteAsync(SqlQuery.PERSISTED_GRANT_UPDATE, new
                {
                    persistedGrant.ClientId,
                    persistedGrant.SubjectId,
                    persistedGrant.Key,
                    persistedGrant.Data,
                    persistedGrant.Type,
                    persistedGrant.CreationTime,
                    persistedGrant.Expiration,
                    UserUpdated = userId
                });

                if (result.IsSuccess)
                {
                    if (result.Data > 0)
                    {
                        return(await Ok(true));
                    }

                    return(await Fail <bool>(string.Format(ErrorEnum.SqlQueryCanNotExecute.GetStringValue(), "PERSISTED_GRANT_UPDATE")));
                }

                return(await Fail <bool>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
        public async Task <TResponse <bool> > Insert(Models.LogAccount logAccount)
        {
            try
            {
                string sqlQuery = @"INSERT dbo.LogAccount (UserId, UserAgentId, IpAddress, Datetime) VALUES (@UserId, @UserAgentId, @IpAddress, @Datetime)";
                var    result   = await WriteRepository.ExecuteAsync(sqlQuery,
                                                                     new
                {
                    logAccount.UserId,
                    logAccount.UserAgentId,
                    logAccount.IpAddress,
                    logAccount.DateTime
                });

                if (result.IsSuccess)
                {
                    if (result.Data > 0)
                    {
                        return(await Ok(true));
                    }

                    return(await Fail <bool>("Can not insert log account"));
                }

                return(await Fail <bool>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 6
0
        public async Task <TResponse <bool> > Update(int userId,
                                                     UpdateCrmPriorityModel request,
                                                     int permissionId)
        {
            try
            {
                var checkValid = await _userService.CheckPermission(userId,
                                                                    permissionId);

                if (checkValid.IsSuccess)
                {
                    var canUpdate = await CanUpdate(request);

                    if (canUpdate.IsSuccess)
                    {
                        var result = await WriteRepository.ExecuteAsync(SqlQuery.CRM_PRIORITY_UPDATE,
                                                                        new
                        {
                            request.Id,
                            request.Name,
                            request.Color,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

                        if (result.IsSuccess)
                        {
                            if (result.Data > 0)
                            {
                                #region Update redis cache

                                await _crmPriorityCacheService.AddOrUpdate(new CrmPriorityCacheModel
                                {
                                    Id    = request.Id,
                                    Name  = request.Name,
                                    Color = request.Color
                                });

                                #endregion

                                return(await Ok(true));
                            }

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

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

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

                return(await Fail <bool>(checkValid.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 7
0
        public async Task <TResponse <bool> > Add(int userId,
                                                  InsertProductModel request,
                                                  int permissionId)
        {
            try
            {
                var checkValid = await _userService.CheckPermission(userId,
                                                                    permissionId);

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

                    if (canInsert.IsSuccess)
                    {
                        var result = await WriteRepository.ExecuteAsync(SqlQuery.PRODUCT_INSERT,
                                                                        new
                        {
                            request.Name,
                            request.ImportPrice,
                            request.SellPrice,
                            request.MinCount,
                            request.MaxCount,
                            request.WarrantyMonth,
                            request.ProductUnitId,
                            request.ProductGroupId,
                            request.ProductStatusId,
                            request.ManufacturerId,
                            request.CountryId,
                            request.Description,
                            UserCreated = userId,
                            DateCreated = DateTime.Now,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

                        if (result.IsSuccess)
                        {
                            if (result.Data > 0)
                            {
                                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));
            }
        }
Exemplo n.º 8
0
        public async Task <TResponse <bool> > Delete(int userId,
                                                     int id,
                                                     int permissionId)
        {
            try
            {
                var checkValid = await CheckPermission(userId,
                                                       permissionId);

                if (checkValid.IsSuccess)
                {
                    var canDelete = await CanDelete(id);

                    if (canDelete.IsSuccess)
                    {
                        var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_DELETE,
                                                                        new
                        {
                            Id          = id,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

                        if (result.IsSuccess)
                        {
                            if (result.Data > 0)
                            {
                                //TODO: xoa file avatar

                                #region Update redis cache

                                await _userCacheService.Remove(id);

                                #endregion

                                return(await Ok(true));
                            }

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

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

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

                return(await Fail <bool>(checkValid.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 9
0
        public async Task <TResponse <bool> > Add(int userId,
                                                  InsertRoleModel request,
                                                  int permissionId)
        {
            try
            {
                var checkValid = await _userService.CheckPermission(userId,
                                                                    permissionId);

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

                    if (canInsert.IsSuccess)
                    {
                        var result = await WriteRepository.ExecuteAsync(SqlQuery.ROLE_INSERT,
                                                                        new
                        {
                            request.Name,
                            UserCreated = userId,
                            DateCreated = DateTime.Now,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

                        if (result.IsSuccess)
                        {
                            if (result.Data > 0)
                            {
                                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));
            }
        }
Exemplo n.º 10
0
        /// <summary>
        ///     Delete User
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="userIsDeletedId"></param>
        /// <returns></returns>
        public async Task <TResponse <bool> > Delete(int userId,
                                                     int userIsDeletedId)
        {
            try
            {
                var canDelete = await CanDelete(userIsDeletedId);

                if (canDelete.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_DELETE, new
                    {
                        Id          = userIsDeletedId,
                        UserUpdated = userId
                    });

                    if (result != null)
                    {
                        if (result.IsSuccess)
                        {
                            if (result.Data == 0)
                            {
                                return(await Fail <bool>($"Delete USER {userIsDeletedId} is failure"));
                            }

                            return(await Ok(true));
                        }

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

                    return(await Fail <bool>($"DELETE USER {userIsDeletedId} is failure"));
                }

                return(await Fail <bool>(canDelete.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 11
0
        public async Task <TResponse <bool> > ChangePassword(int userId,
                                                             string password,
                                                             string newPassword)
        {
            try
            {
                var canChangePassword = await CanChangePassword(userId,
                                                                password);

                if (canChangePassword.IsSuccess)
                {
                    newPassword = Sha512(newPassword);
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_CHANGE_PASSWORD,
                                                                    new
                    {
                        Id       = userId,
                        Password = newPassword
                    });

                    if (result.IsSuccess)
                    {
                        if (result.Data > 0)
                        {
                            return(await Ok(true));
                        }

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

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

                return(await Fail <bool>(canChangePassword.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 12
0
        public async Task <TResponse <bool> > ChangePassword(string username,
                                                             string currentPassword,
                                                             string password)
        {
            try
            {
                var checkPassword = await CheckInvalidPassword(username, currentPassword);

                if (checkPassword.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_UPDATED_PASSWORD, new
                    {
                        Id          = checkPassword.Data,
                        Password    = Sha512(password),
                        UserUpdated = checkPassword.Data
                    });

                    if (result.IsSuccess)
                    {
                        if (result.Data > 0)
                        {
                            return(await Ok(true));
                        }

                        return(await Fail <bool>(string.Format(ErrorEnum.SqlQueryCanNotExecute.GetStringValue(), "USER_UPDATED_PASSWORD")));
                    }

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

                return(await Fail <bool>(checkPassword.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 13
0
        public async Task <TResponse <bool> > UpdateInformation(UserDto user)
        {
            try
            {
                var canUpdate = await GetById(user.Id);

                if (canUpdate.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_UPDATE_INFORMATION, new
                    {
                        user.Id,
                        user.Email,
                        user.Name,
                        user.Phone,
                        UserUpdated = user.Id
                    });

                    if (result.IsSuccess)
                    {
                        if (result.Data > 0)
                        {
                            return(await Ok(true));
                        }

                        return(await Fail <bool>(string.Format(ErrorEnum.SqlQueryCanNotExecute.GetStringValue(), "USER_UPDATE_INFORMATION")));
                    }

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

                return(await Fail <bool>(canUpdate.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 14
0
        /// <summary>
        ///     Change User Password
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="id"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public async Task <TResponse <bool> > ChangePassword(int userId,
                                                             int id,
                                                             string password)
        {
            try
            {
                var canChangePassword = await CanChangePassword(id);

                if (canChangePassword.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_CHANGE_PASSWORD, new
                    {
                        Id          = id,
                        Password    = Sha512(password),
                        UserUpdated = userId
                    });

                    if (result.IsSuccess)
                    {
                        if (result.Data > 0)
                        {
                            return(await Ok(true));
                        }

                        return(await Fail <bool>(string.Format(ErrorEnum.SqlQueryCanNotExecute.GetStringValue(), "USER_CHANGE_PASSWORD")));
                    }

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

                return(await Fail <bool>(canChangePassword.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 15
0
        /// <summary>
        ///     Update Site
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="request"></param>
        /// <returns></returns>
        public async Task <TResponse <bool> > Update(int userId,
                                                     UpdateSiteModel request)
        {
            try
            {
                var canUpdate = await CanUpdate(request);

                if (canUpdate.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.SITE_UPDATE, new
                    {
                        request.Id,
                        request.SiteCode,
                        request.SiteName,
                        UserUpdated = userId
                    });

                    if (result.IsSuccess)
                    {
                        if (result.Data == 0)
                        {
                            return(await Fail <bool>(string.Format(ErrorEnum.SqlQueryCanNotExecute.GetStringValue(), "SITE_UPDATE")));
                        }

                        return(await Ok(true));
                    }

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

                return(await Fail <bool>(canUpdate.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 16
0
        public async Task<TResponse<bool>> Add(InsertNotificationModel request)
        {
            try
            {
                var result = await WriteRepository.ExecuteAsync(SqlQuery.NOTIFICATION_INSERT,
                                                                new
                                                                {
                                                                        request.UserId,
                                                                        request.UserReceiveId,
                                                                        request.Title,
                                                                        request.Message,
                                                                        request.Action,
                                                                        request.Type,
                                                                        request.Status,
                                                                        DateCreated = DateTime.Now,
                                                                        DateUpdated = request.Status == 0
                                                                                              ? null
                                                                                              : (DateTime?) DateTime.Now
                                                                });
                if(result.IsSuccess)
                {
                    if(result.Data > 0)
                    {
                        return await Ok(true);
                    }

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

                return await Fail<bool>(result.Message);
            }
            catch (Exception exception)
            {
                return await Fail<bool>(exception);
            }
        }
Exemplo n.º 17
0
        public async Task <TResponse <bool> > UpdateInformationUser(int userId,
                                                                    UpdateInformationModel request,
                                                                    string folder)
        {
            try
            {
                var user = await GetById(userId);

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

                    var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_UPDATE_INFORMATION,
                                                                    new
                    {
                        request.FullName,
                        request.DisplayName,
                        request.PhoneNumber,
                        request.Email,
                        request.BranchId,
                        request.Avatar,
                        UserUpdated = userId,
                        DateUpdated = DateTime.Now,
                        Id          = userId
                    });

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

                                    using (FileStream filestream = File.Create(Path.Combine(folder,
                                                                                            user.Data.Username) + ".png"))
                                    {
                                        image.CopyTo(filestream);
                                        filestream.Flush();
                                    }
                                }
                                catch (Exception ex)
                                {
                                }
                            }

                            return(await Ok(true));
                        }

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

                return(await Fail <bool>(user.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 18
0
        public async Task <TResponse <bool> > Log(AuditTableKafkaMessage <AuditCrm> crm)
        {
            try
            {
                string title = string.Empty;
                switch (crm.LogType)
                {
                case LogTypeEnum.INSERT:
                    title = "đã thêm mới Cơ hội bán hàng";
                    break;

                case LogTypeEnum.UPDATE:
                    title = "đã cập nhật cơ hội bán hàng";
                    break;

                case LogTypeEnum.CHANGE_STATUS:
                    title = "đã thay đổi trạng thái";
                    break;

                case LogTypeEnum.COMMENT:
                    title = "đã bình luận";
                    break;
                }

                string message = await BuildMessage(crm);

                var result = await WriteRepository.ExecuteScalarAsync <int>(SqlQuery.INSERT_LOG, new
                {
                    TableName = "Crm",
                    ObjectId  = crm.Id,
                    Title     = title,
                    Message   = message,
                    crm.UserId,
                    LogDate     = DateTime.Now,
                    OldStatusId = crm.OldValue.CrmStatusId,
                    StatusId    = crm.Value.CrmStatusId
                });

                if (result.IsSuccess)
                {
                    if (result.Data > 0)
                    {
                        if (crm.Attachments.Any())
                        {
                            foreach (var crmAttachment in crm.Attachments)
                            {
                                await WriteRepository.ExecuteAsync(SqlQuery.INSERT_LOG_ATTACHMENTS, new
                                {
                                    TableLogId     = result.Data,
                                    Attachment     = crmAttachment.Key,
                                    AttachmentName = crmAttachment.Value
                                });
                            }
                        }

                        Logger.Info($"Insert log CRM: {title} - {message}");

                        return(await Ok(true));
                    }

                    return(await Fail <bool>($"Can not insert log: {crm.ToJson()}"));
                }

                return(await Fail <bool>(result.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 19
0
        /// <summary>
        ///     Update User
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <TResponse <bool> > Update(int userId,
                                                     UpdateUserModel user)
        {
            try
            {
                var canUpdate = await CanUpdate(user);

                if (canUpdate.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_UPDATE, new
                    {
                        user.Id,
                        user.Email,
                        user.Name,
                        user.Phone,
                        UserUpdated = userId
                    });

                    if (result != null)
                    {
                        if (result.IsSuccess)
                        {
                            if (result.Data == 0)
                            {
                                return(await Fail <bool>($"Update USER {user.Id} is failure"));
                            }

                            #region Set Sites

                            var sites = user.Sites ?? new List <int>();

                            var oldSites = await ReadOnlyRepository.QueryAsync <SiteModel>(SqlQuery.SITE_GET_BY_USER_ID, new
                            {
                                UserId = user.Id
                            });

                            //kiem tra site nao chua ton tai thi them moi
                            foreach (var site in sites)
                            {
                                var exist = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.USER_CHECK_SITE_VALID, new
                                {
                                    UserId = user.Id,
                                    SiteId = site
                                });

                                if (exist.IsSuccess)
                                {
                                    if (exist.Data == 0)
                                    {
                                        //chua co, them moi vao
                                        var siteId = await ReadOnlyRepository.QueryFirstOrDefaultAsync <SiteModel>(SqlQuery.SITE_GET_BY_ID, new
                                        {
                                            Id = site
                                        });

                                        if (siteId.IsSuccess &&
                                            siteId.Data != null)
                                        {
                                            await WriteRepository.ExecuteAsync(SqlQuery.USER_SITE_INSERT, new
                                            {
                                                UserId      = user.Id,
                                                SiteId      = siteId.Data.Id,
                                                UserCreated = userId,
                                                UserUpdated = userId
                                            });
                                        }
                                    }
                                }
                            }

                            //kiem tra site nao bi xoa bo
                            if (oldSites.IsSuccess)
                            {
                                if (oldSites.Data != null &&
                                    oldSites.Data.Any())
                                {
                                    foreach (var oldSite in oldSites.Data)
                                    {
                                        if (!sites.Contains(oldSite.Id))
                                        {
                                            await WriteRepository.ExecuteAsync(SqlQuery.REMOVE_USER_FROM_SITE, new
                                            {
                                                UserId      = user.Id,
                                                SiteId      = oldSite.Id,
                                                UserUpdated = userId
                                            });
                                        }
                                    }
                                }
                            }

                            #endregion

                            #region Set Roles

                            var roles = user.Roles ?? new List <int>();

                            var oldRoles = await ReadOnlyRepository.QueryAsync <RoleModel>(SqlQuery.ROLE_GET_BY_USER_ID, new
                            {
                                UserId = user.Id
                            });

                            //kiem tra role nao chua ton tai thi them moi
                            foreach (var role in roles)
                            {
                                var exist = await ReadOnlyRepository.QueryFirstOrDefaultAsync <int>(SqlQuery.USER_CHECK_ROLE_VALID, new
                                {
                                    UserId = user.Id,
                                    RoleId = role
                                });

                                if (exist.IsSuccess)
                                {
                                    if (exist.Data == 0)
                                    {
                                        //chua co, them moi vao
                                        var roleId = await ReadOnlyRepository.QueryFirstOrDefaultAsync <RoleModel>(SqlQuery.ROLE_GET_BY_ID, new
                                        {
                                            Id = role
                                        });

                                        if (roleId.IsSuccess &&
                                            roleId.Data != null)
                                        {
                                            await WriteRepository.ExecuteAsync(SqlQuery.USER_ROLE_INSERT, new
                                            {
                                                UserId      = user.Id,
                                                RoleId      = roleId.Data.Id,
                                                UserCreated = userId,
                                                UserUpdated = userId
                                            });
                                        }
                                    }
                                }
                            }

                            //kiem tra role nao bi xoa bo
                            if (oldRoles.IsSuccess)
                            {
                                if (oldRoles.Data != null &&
                                    oldRoles.Data.Any())
                                {
                                    foreach (var oldRole in oldRoles.Data)
                                    {
                                        if (!roles.Contains(oldRole.Id))
                                        {
                                            await WriteRepository.ExecuteAsync(SqlQuery.REMOVE_USER_FROM_ROLE, new
                                            {
                                                UserId      = user.Id,
                                                RoleId      = oldRole.Id,
                                                UserUpdated = userId
                                            });
                                        }
                                    }
                                }
                            }

                            #endregion

                            return(await Ok(true));
                        }

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

                    return(await Fail <bool>($"Update USER {user.Id} is failure"));
                }

                return(await Fail <bool>(canUpdate.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
        public async Task Update(int crmId,
                                 List <int> userIds,
                                 int type,
                                 int userUpdated)
        {
            var crmUsers = await LoadFromRedis();

            var existUserIds = crmUsers.Where(c => c.CrmId == crmId && c.Type == type)
                               .Select(c => c.UserId);

            foreach (var userId in userIds)
            {
                if (!existUserIds.Contains(userId))
                {
                    //them moi
                    var result = await WriteRepository.ExecuteScalarAsync <int>(SqlQuery.INSERT_CRM_USER,
                                                                                new
                    {
                        CrmId       = crmId,
                        UserId      = userId,
                        Type        = type,
                        UserCreated = userUpdated,
                        DateCreated = DateTime.Now,
                        UserUpdated = userUpdated,
                        DateUpdated = DateTime.Now
                    });

                    if (result.IsSuccess)
                    {
                        if (result.Data > 0)
                        {
                            crmUsers.Add(new CrmUserCacheModel
                            {
                                Id     = result.Data,
                                CrmId  = crmId,
                                UserId = userId,
                                Type   = type
                            });
                        }
                    }
                }
            }

            foreach (var existUserId in existUserIds)
            {
                if (!userIds.Contains(existUserId))
                {
                    //xoa bo
                    var result = await WriteRepository.ExecuteAsync(SqlQuery.REMOVE_CRM_USER,
                                                                    new
                    {
                        CrmId       = crmId,
                        UserId      = existUserId,
                        Type        = type,
                        UserUpdated = userUpdated,
                        DateUpdated = DateTime.Now
                    });

                    if (result.IsSuccess)
                    {
                        if (result.Data > 0)
                        {
                            var crmUser = crmUsers.FirstOrDefault(c => c.CrmId == crmId && c.UserId == existUserId && c.Type == type);
                            if (crmUser != null)
                            {
                                crmUsers.Remove(crmUser);
                            }
                        }
                    }
                }
            }

            await DistributedCache.Set(_cacheKey,
                                       crmUsers);
        }
Exemplo n.º 21
0
        public async Task <TResponse <bool> > Update(int userId,
                                                     UpdateBranchModel request,
                                                     int permissionId)
        {
            try
            {
                var checkValid = await _userService.CheckPermission(userId,
                                                                    permissionId);

                if (checkValid.IsSuccess)
                {
                    var canUpdate = await CanUpdate(request);

                    if (canUpdate.IsSuccess)
                    {
                        var result = await WriteRepository.ExecuteAsync(SqlQuery.BRANCH_UPDATE,
                                                                        new
                        {
                            request.Id,
                            request.Name,
                            request.Hotline,
                            request.Address,
                            request.ProvinceId,
                            request.DistrictId,
                            request.WardId,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

                        if (result.IsSuccess)
                        {
                            if (result.Data > 0)
                            {
                                #region Update redis cache

                                await _branchCacheService.AddOrUpdate(new BranchCacheModel
                                {
                                    Id         = request.Id,
                                    Name       = request.Name,
                                    Hotline    = request.Hotline,
                                    ProvinceId = request.ProvinceId,
                                    DistrictId = request.DistrictId,
                                    WardId     = request.WardId,
                                    Address    = request.Address
                                });

                                #endregion

                                return(await Ok(true));
                            }

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

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

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

                return(await Fail <bool>(checkValid.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 22
0
        public async Task <TResponse <bool> > Update(int userId,
                                                     UpdateWarrantyStatusModel request,
                                                     int permissionId)
        {
            try
            {
                var checkValid = await _userService.CheckPermission(userId,
                                                                    permissionId);

                if (checkValid.IsSuccess)
                {
                    var canUpdate = await CanUpdate(request);

                    if (canUpdate.IsSuccess)
                    {
                        var result = await WriteRepository.ExecuteAsync(SqlQuery.WARRANTY_STATUS_UPDATE,
                                                                        new
                        {
                            request.Id,
                            request.Name,
                            request.Color,
                            request.SortOrder,
                            request.IsSendSms,
                            request.SmsContent,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

                        if (result.IsSuccess)
                        {
                            if (result.Data > 0)
                            {
                                #region Update redis cache

                                await _warrantyStatusCacheService.AddOrUpdate(new WarrantyStatusCacheModel
                                {
                                    Id         = request.Id,
                                    Name       = request.Name,
                                    Color      = request.Color,
                                    SortOrder  = request.SortOrder,
                                    SmsContent = request.SmsContent,
                                    IsSendSms  = request.IsSendSms
                                });

                                #endregion

                                return(await Ok(true));
                            }

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

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

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

                return(await Fail <bool>(checkValid.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 23
0
        public async Task <TResponse <bool> > Update(int userId,
                                                     UpdateUserModel request,
                                                     int permissionId,
                                                     string path)
        {
            try
            {
                var checkValid = await CheckPermission(userId,
                                                       permissionId);

                if (checkValid.IsSuccess)
                {
                    var canUpdate = await CanUpdate(request);

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

                        var result = await WriteRepository.ExecuteAsync(SqlQuery.USER_UPDATE,
                                                                        new
                        {
                            request.Id,
                            request.DisplayName,
                            request.FullName,
                            request.Email,
                            request.PhoneNumber,
                            request.BranchId,
                            request.RoleId,
                            Avatar      = avatar,
                            UserUpdated = userId,
                            DateUpdated = DateTime.Now
                        });

                        if (result.IsSuccess)
                        {
                            if (result.Data > 0)
                            {
                                string message = string.Empty;

                                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 ex)
                                    {
                                        message = ex.ToString();
                                    }
                                }

                                #region Update redis cache

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

                                #endregion

                                return(await Ok(true,
                                                message));
                            }

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

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

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

                return(await Fail <bool>(checkValid.Message));
            }
            catch (Exception exception)
            {
                return(await Fail <bool>(exception));
            }
        }
Exemplo n.º 24
0
        /// <summary>
        ///     Add User
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="user"></param>
        /// <returns></returns>
        public async Task <TResponse <bool> > Add(int userId,
                                                  InsertUserModel user)
        {
            try
            {
                var canInsert = await CanInsert(user);

                if (canInsert.IsSuccess)
                {
                    var result = await WriteRepository.ExecuteScalarAsync <int>(SqlQuery.USER_INSERT, new
                    {
                        user.Username,
                        user.Email,
                        user.Name,
                        user.Phone,
                        Password    = Sha512(user.Password),
                        UserCreated = userId,
                        UserUpdated = userId
                    });

                    if (result != null)
                    {
                        if (result.IsSuccess)
                        {
                            if (result.Data == 0)
                            {
                                return(await Fail <bool>($"Insert USER {user.Username} is failure"));
                            }

                            var insertedUserId = result.Data;

                            #region Insert site

                            if (user.Sites != null &&
                                user.Sites.Any())
                            {
                                foreach (var siteId in user.Sites)
                                {
                                    var site = await ReadOnlyRepository.QueryFirstOrDefaultAsync <SiteModel>(SqlQuery.SITE_GET_BY_ID, new
                                    {
                                        Id = siteId
                                    });

                                    if (site.IsSuccess &&
                                        site.Data != null)
                                    {
                                        await WriteRepository.ExecuteAsync(SqlQuery.USER_SITE_INSERT, new
                                        {
                                            UserId      = insertedUserId,
                                            SiteId      = siteId,
                                            UserCreated = userId,
                                            UserUpdated = userId
                                        });
                                    }
                                }
                            }

                            #endregion

                            #region Insert role

                            if (user.Roles != null &&
                                user.Roles.Any())
                            {
                                foreach (var roleId in user.Roles)
                                {
                                    var role = await ReadOnlyRepository.QueryFirstOrDefaultAsync <RoleModel>(SqlQuery.ROLE_GET_BY_ID, new
                                    {
                                        Id = roleId
                                    });

                                    if (role.IsSuccess &&
                                        role.Data != null)
                                    {
                                        await WriteRepository.ExecuteAsync(SqlQuery.ROLE_GROUP_INSERT_USER, new
                                        {
                                            UserId      = insertedUserId,
                                            RoleId      = roleId,
                                            UserCreated = userId,
                                            UserUpdated = userId
                                        });
                                    }
                                }
                            }

                            #endregion

                            return(await Ok(true));
                        }

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

                    return(await Fail <bool>($"Insert USER {user.Username} is failure"));
                }

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