Exemple #1
0
        public async Task <IActionResult> AddUserAsync([FromBody] UserRequestDto userRequest)
        {
            try
            {
                if (userRequest != null)
                {
                    var userView = new UserView
                    {
                        LoginName = userRequest.LoginName,
                        Password  = userRequest.Password,
                        EmailId   = userRequest.EmailId,
                        IsActive  = userRequest.IsActive
                    };

                    var roleId = _roleViewRepository.GetRoles().Where(i => i.RoleName == userRequest.RoleName)?.Select(j => j.RoleId).FirstOrDefault();
                    if (roleId == null || roleId == 0)
                    {
                        return(StatusCode(400, new ErrorModel {
                            Message = "Invalid Role", HttpStatusCode = 400
                        }));
                    }
                    userView.RoleId = roleId.Value;

                    if (!string.IsNullOrWhiteSpace(userRequest.UserCode))
                    {
                        if (userRequest.RoleName == Roles.GroupUser.ToString())
                        {
                            userView.GroupName = userRequest.UserCode;
                        }
                        else if (userRequest.RoleName == Roles.Dealer.ToString())
                        {
                            userView.DealerCode = userRequest.UserCode;
                        }
                        else if (userRequest.RoleName == Roles.Client.ToString())
                        {
                            userView.ClientCode = userRequest.UserCode;
                        }
                    }

                    if (!string.IsNullOrWhiteSpace(userView.ClientCode) && !_userViewRepository.GetClientViews().Select(i => i.ClientCode).Contains(userView.ClientCode))
                    {
                        return(StatusCode(400, new ErrorModel {
                            Message = "Invalid ClientCode", HttpStatusCode = 400
                        }));
                    }
                    if (!string.IsNullOrWhiteSpace(userView.GroupName) && !_userViewRepository.GetGroups().Select(i => i.GroupName).Contains(userView.GroupName))
                    {
                        return(StatusCode(400, new ErrorModel {
                            Message = "Invalid GroupCode", HttpStatusCode = 400
                        }));
                    }
                    if (!string.IsNullOrWhiteSpace(userView.DealerCode) && !_userViewRepository.GetDealers().Select(i => i.DealerCode).Contains(userView.DealerCode))
                    {
                        return(StatusCode(400, new ErrorModel {
                            Message = "Invalid DealerCode", HttpStatusCode = 400
                        }));
                    }

                    await _userViewRepository.AddUserAsync(userView);

                    return(Ok());
                }
                else
                {
                    return(StatusCode(400, new ErrorModel {
                        Message = "Bad Parameter Passed", HttpStatusCode = 400
                    }));
                }
            }
            catch (Exception ex)
            {
                return(StatusCode(500, new ErrorModel {
                    Message = ex.Message, HttpStatusCode = 500
                }));
            }
        }