Пример #1
0
        public static User CreateUser(UserRegisterDto registerDto)
        {
            var newUser = new User()
            {
                UserName = registerDto.UserName.ToLower(),
                NickName = registerDto.NickName,
                AuthCode = registerDto.AuthCode
            };

            return newUser;
        }
Пример #2
0
        [ActionName("register")] //api/users/register
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            try
            {
                var dbContext = new NationalPlacesContext();
                using (dbContext)
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateName(model.Name);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameToLower = model.Username.ToLower();
                    var user            = dbContext.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower);

                    if (user != null)
                    {
                        throw new InvalidOperationException("Username is already taken!");
                    }

                    user = new NationalPlaces.Models.User()
                    {
                        Username          = usernameToLower,
                        Name              = model.Name,
                        AuthCode          = model.AuthCode,
                        ProfilePictureUrl = model.ProfilePictureUrl
                    };

                    dbContext.Users.Add(user);
                    dbContext.SaveChanges();

                    user.SessionKey = this.GenerateSessionKey(user.Id);
                    dbContext.SaveChanges();

                    var loggedModel = new LoggedUserModel()
                    {
                        Name       = user.Name,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created,
                                                               loggedModel);
                    return(response);
                }
            }
            catch (Exception ex)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.BadRequest,
                                                           ex.Message);
                return(response);
            }
        }
Пример #3
0
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            try
            {
                var dbContext = new NationalPlacesContext();
                using (dbContext)
                {
                    this.ValidateUsername(model.Username);
                    this.ValidateName(model.Name);
                    this.ValidateAuthCode(model.AuthCode);

                    var usernameToLower = model.Username.ToLower();
                    var user = dbContext.Users.FirstOrDefault(u => u.Username.ToLower() == usernameToLower);

                    if (user != null)
                    {
                        throw new InvalidOperationException("Username is already taken!");
                    }

                    user = new NationalPlaces.Models.User()
                    {
                        Username = usernameToLower,
                        Name = model.Name,
                        AuthCode = model.AuthCode,
                        ProfilePictureUrl = model.ProfilePictureUrl

                    };

                    dbContext.Users.Add(user);
                    dbContext.SaveChanges();

                    user.SessionKey = this.GenerateSessionKey(user.Id);
                    dbContext.SaveChanges();

                    var loggedModel = new LoggedUserModel()
                    {
                        Name = user.Name,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created,
                                              loggedModel);
                    return response;
                }
            }
            catch (Exception ex)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.BadRequest,
                                             ex.Message);
                return response;
            }
        }