Ejemplo n.º 1
0
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            try
            {
                using (var context = new OfferWorldContext())
                {
                    Validator.ValidateUsername(model.Username);
                    Validator.ValidateAuthCode(model.AuthCode);
                    Validator.ValidateEmail(model.Email);

                    var usernameToLower = model.Username.ToLower();
                    var username = (context.Users.FirstOrDefault(usr => usr.Username == usernameToLower || usr.Email == model.Email));
                    
                    if (username != null)
                    {
                        throw new ArgumentException("Username or Email already exist");
                    }

                    var user = new User()
                    {
                        Username = model.Username,
                        AuthCode = model.AuthCode, 
                        Email = model.Email,
                        Admin = false
                    };

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

                    user.SessionKey = this.GenerateSessionKey(user.UserId);

                    context.SaveChanges();

                    var loggedModel = new UserLoggedModel()
                    {
                        Username = user.Username,
                        SessionKey = user.SessionKey
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel);
                    return response;
                }
            }
            catch (Exception ex)
            {
                var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
                return errResponse;
            }
        }
Ejemplo n.º 2
0
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            try
            {
                using (var context = new OfferWorldContext())
                {
                    Validator.ValidateUsername(model.Username);
                    Validator.ValidateAuthCode(model.AuthCode);

                    var usernameToLower = model.Username.ToLower();
                    var user = (context.Users.FirstOrDefault(usr => usr.Username == usernameToLower &&
                            usr.AuthCode == model.AuthCode));

                    if (user == null)
                    {
                        throw new ArgumentException("Username does not exist!");
                    }
                    else if (user.SessionKey == null)
                    {
                        user.SessionKey = this.GenerateSessionKey(user.UserId);
                        context.SaveChanges();
                    }

                    var loggedModel = new UserLoggedModel()
                    {
                        Username = user.Username,
                        SessionKey = user.SessionKey
                    };

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