public HttpResponseMessage LogoutUser( [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { return this.ExecuteOperationAndHandleExceptions(() => { var context = new GameContext(); var user = UserPersister.GetUserBySessionKey(sessionKey, context); user.SessionKey = null; context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.NoContent); return response; }); }
public HttpResponseMessage LoginUser(LoginUserModel model) { return this.ExecuteOperationAndHandleExceptions(() => { if (model == null) { throw new FormatException("invalid username and/or password"); } UserPersister.ValidateLoginUser(model); var context = new GameContext(); var user = context.Users.FirstOrDefault(u => u.Username == model.Username.ToLower() && u.AuthCode == model.AuthCode); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } if (user.SessionKey == null) { user.SessionKey = UserPersister.GenerateSessionKey(user.Id); context.SaveChanges(); } var responseModel = new UserResponseModel() { Nickname = user.Nickname, SesionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.OK, responseModel); return response; }); }
public HttpResponseMessage CreateHeroe(CreateHeroeModel model, [ValueProvider(typeof(HeaderValueProviderFactory<string>))] string sessionKey) { return this.ExecuteOperationAndHandleExceptions(() => { var context = new GameContext(); var user = BasePersister.GetUserBySessionKey(sessionKey, context); if (user == null) { throw new InvalidOperationException("Invalid username or password!"); } HeroePersister.ValidateCreateHeroe(model, context); Hero newHeroe = new Hero() { Name = model.Name, Race = context.Races.First(r => r.Id == model.Race), Level = StartHeroeLevel, Points = StartHeroePoints, Money = StartHeroeMoney, NumberOfLoses = 0, NumberOfWins = 0, User = BasePersister.GetUserBySessionKey(sessionKey, context), Units = new HashSet<Unit>() }; context.Heroes.Add(newHeroe); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.NoContent); return response; }); }
public HttpResponseMessage RegisterUser(RegisterUserModel model) { return this.ExecuteOperationAndHandleExceptions(() => { var context = new GameContext(); var dbUser = UserPersister.GetUserByUsernameAndDisplayName(model.Username, model.Nickname, context); if (dbUser != null) { throw new InvalidOperationException("This user already exists in the database"); } dbUser = new User() { Username = model.Username.ToLower(), Nickname = model.Nickname, AuthCode = model.AuthCode, Role = context.Roles.Where(r => r.Name == "user").FirstOrDefault() }; context.Users.Add(dbUser); dbUser.SessionKey = UserPersister.GenerateSessionKey(dbUser.Id); context.SaveChanges(); var responseModel = new UserResponseModel() { Nickname = dbUser.Nickname, SesionKey = dbUser.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, responseModel); return response; }); }
public HttpResponseMessage UploadAvatar() { return this.ExecuteOperationAndHandleExceptions(() => { var httpRequest = HttpContext.Current.Request; var context = new GameContext(); var dbUser = UserPersister.GetUserByUsernameAndDisplayName(httpRequest.Form["username"], httpRequest.Form["nickname"], context); if (dbUser == null) { throw new InvalidOperationException("This user already exists in the database"); } if (httpRequest.Files.Count > 0) { foreach (string file in httpRequest.Files) { var postedFile = httpRequest.Files[file]; var configuration = new AccountConfiguration("djlwcsyiz", "781383948985498", "Vh5BQmeTxvSKvTGTg-wRDYKqPz4"); var uploader = new Uploader(configuration); string publicId = Path.GetFileNameWithoutExtension(postedFile.FileName); var uploadResult = uploader.Upload(new UploadInformation(postedFile.FileName, postedFile.InputStream) { PublicId = publicId, Format = postedFile.FileName.Substring(postedFile.FileName.Length - 3), }); dbUser.Avatar = uploadResult.Url; } } context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.NoContent); return response; }); }