public async Task <ServiceResult> RegisterAsync(RegisterDto model) { var result = new ServiceResult() { Status = EServiceResultStatus.Success }; try { var userExist = await _uow.Repository <User>().Get() .AnyAsync(u => u.Email == model.Email.ToLower()); if (userExist) { return(new ServiceResult { Status = EServiceResultStatus.Error, Message = StringConstants.UserAlreadyExist }); } await _uow.BeginTransactionAsync(); var newUser = model.ToEntity(); await _uow.Repository <User>().AddAsync(newUser); await _uow.SaveAsync(); _uow.CommitTransaction(); } catch (Exception e) { _logger.LogException(e); result.Status = EServiceResultStatus.Error; result.Message = StringConstants.DatabaseError; } return(result); }
public async Task <ServiceResult> Register(RegisterDto dto) { var serviceResult = dto.IsValid(); if (serviceResult.IsSuccess) { if (ExistUserByPhone(dto.PhoneNumber)) { serviceResult.AddError("شماره همراه وارد قبلا ثبت نام کرده است"); } else { var user = dto.ToEntity(); user.RegisterDate = DateTime.Now; user.Type = UserType.Manual; var identityResult = await _userManager.CreateAsync(user, dto.Password); if (identityResult.Succeeded) { serviceResult.Message = "یک کاربر با موفقیت ثبت نام شد"; } else { identityResult.Errors.ToList().ForEach(c => { serviceResult.AddError(c.Code); }); } } } return(serviceResult); }
public User AddUser(string role, RegisterDto dto) { var user = dto.ToEntity(); user.Password = _passwordHasher.HashPassword(user, dto.Password); Add(user); var userRole = new UserRole { RoleId = Roles.SingleOrDefault(x => x.Name.Equals(role)).Id, UserId = user.Id }; UserRoles.Add(userRole); return(user); }