Esempio n. 1
0
        public HttpResponseMessage PostRegisterUser(UserModel model)
        {
            return this.PerformOperationAndHandleExceptions(() =>
            {
                ValidateNickname(model.Nickname);
                ValidateUsername(model.Username);
                ValidateAuthCode(model.AuthCode);
                var usernameToLower = model.Username.ToLower();
                var context = this.ContextFactory.Create();
                using (context)
                {
                    var usersDbSet = context.Set<User>();
                    var entity = usersDbSet
                        .FirstOrDefault(usr => usr.Username == usernameToLower);
                    if (entity != null)
                    {
                        string responseMessage = "Username already taken";
                        HttpResponseMessage errResponse = this.Request.CreateErrorResponse(HttpStatusCode.Conflict, responseMessage);
                        throw new HttpResponseException(errResponse);
                    }

                    entity = new User()
                    {
                        Nickname = model.Nickname,
                        Username = model.Username.ToLower(),
                        AuthCode = model.AuthCode,
                        Amount = 100,
                    };

                    var role = context.Set<Role>().FirstOrDefault(r => r.Permission == "registered");
                    entity.Role = role;
                    entity.SessionKey = this.GenerateSessionKey(entity.Id);
                    usersDbSet.Add(entity);
                    context.SaveChanges();

                    var responseModel = new LoginResponseModel()
                    {
                        Nickname = entity.Nickname,
                        SessionKey = entity.SessionKey,
                        Amount = entity.Amount ?? 0
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel);
                    return response;
                }
            });
        }
Esempio n. 2
0
        public HttpResponseMessage PostLoginUser(UserModel model)
        {
            return this.PerformOperationAndHandleExceptions(() =>
            {
                ValidateAuthCode(model.AuthCode);
                var context = this.ContextFactory.Create();
                using (context)
                {
                    var usernameToLower = model.Username.ToLower();
                    var modetlAutchCode = model.AuthCode;
                    var usersDbSet = context.Set<User>();
                    var entity = usersDbSet.SingleOrDefault(usr => usr.Username == usernameToLower && usr.AuthCode == modetlAutchCode);
                    if (entity == null)
                    {
                        var errResponse = this.Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid username or password");
                        throw new HttpResponseException(errResponse);
                    }

                    entity.SessionKey = this.GenerateSessionKey(entity.Id);


                    context.SaveChanges();

                    //catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
                    //{
                    //    foreach (var validationErrors in dbEx.EntityValidationErrors)
                    //    {
                    //        foreach (var validationError in validationErrors.ValidationErrors)
                    //        {
                    //            Trace.TraceInformation("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);
                    //        }
                    //    }
                    //}

                    var responseModel = new LoginResponseModel()
                    {
                        Nickname = entity.Nickname,
                        SessionKey = entity.SessionKey,
                        Amount = entity.Amount ?? 0
                    };

                    var response = this.Request.CreateResponse(HttpStatusCode.Accepted, responseModel);
                    return response;
                }
            });
        }