Exemple #1
0
        public HttpResponseMessage PostRegister(RegisterModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
            {
                var context = new DrinkBenchContext();
                using (context)
                {
                    //this.ValidateName(model.Firstname);
                    //this.ValidateName(model.Lastname);
                    this.ValidateAuthCode(model.AuthCode);
                    var firstnameToLower = model.Firstname.ToLower();
                    var lastnameToLower  = model.Lastname.ToLower();
                    var user             = context.Users.FirstOrDefault(
                        usr => usr.Firstname.ToLower() == firstnameToLower &&
                        usr.Lastname.ToLower() == lastnameToLower &&
                        usr.Nickname == model.Nickname);

                    if (user != null)
                    {
                        throw new InvalidOperationException("Users exists");
                    }

                    user = new User()
                    {
                        Avatar    = model.Avatar,
                        AuthCode  = model.AuthCode,
                        Firstname = model.Firstname,
                        Lastname  = model.Lastname,
                        Nickname  = model.Nickname,
                        StartTime = DateTime.Now,
                        EndTime   = DateTime.Now
                    };

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

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

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

                    var response =
                        this.Request.CreateResponse(HttpStatusCode.Created,
                                                    loggedModel);
                    return(response);
                }
            });

            return(responseMsg);
        }
Exemple #2
0
        public HttpResponseMessage PostLogin(RegisterModel model)
        {
            var responseMsg = this.PerformOperationAndHandleExceptions(
                () =>
            {
                var context = new DrinkBenchContext();
                using (context)
                {
                    //this.ValidateName(model.Firstname);
                    //this.ValidateName(model.Lastname);
                    this.ValidateAuthCode(model.AuthCode);
                    var firstnameToLower = model.Firstname.ToLower();
                    var user             = context.Users.FirstOrDefault(
                        usr => usr.Firstname.ToLower() == firstnameToLower &&
                        usr.AuthCode == model.AuthCode);

                    if (user == null)
                    {
                        var userWithoutOAuth = context.Users.FirstOrDefault(
                            usr => usr.Firstname.ToLower() == firstnameToLower &&
                            usr.Nickname == model.Nickname);
                        if (userWithoutOAuth != null)
                        {
                            throw new InvalidOperationException(
                                "Users info incorrect Firstname:" + model.Firstname +
                                "AuthCode:" + model.AuthCode);
                        }
                        else
                        {
                            return(PostRegister(model));
                        }
                    }
                    if (user.SessionKey == null)
                    {
                        user.SessionKey = this.GenerateSessionKey(user.Id);
                        context.SaveChanges();
                    }

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

                    var response =
                        this.Request.CreateResponse(HttpStatusCode.Created,
                                                    loggedModel);
                    return(response);
                }
            });

            return(responseMsg);
        }