public ActionResult Delete(int id) { using (GameJamDBContext context = new GameJamDBContext()) { //Find the game by its' primary key Games game = context.Games.Find(id); if (game == null) { return(NotFound("Game was not found")); } // Check whether user is author var authorId = HttpContext.User.Claims.SingleOrDefault(c => c.Type == "id")?.Value; if (game.FkUser != int.Parse(authorId)) { return(BadRequest("You can't delete this game, since you're not the author")); } //Delete the game context.Games.Remove(game); context.SaveChanges(); return(Ok("Game entry deleted successfully")); } }
public ActionResult <List <Games> > GetJamEntries(int id) { using (GameJamDBContext context = new GameJamDBContext()) { Jams jam = context.Jams.Find(id); if (jam == null) { return(BadRequest("Jam was not found")); } else { var entries = context .Games .Include(u => u.FkUserNavigation) .Where((g) => g.FkJam == id) .Select(e => new JamEntry() { IdGame = e.IdGame, ThumbnailImageLink = e.ThumbnailImageLink, Title = e.Title, AuthorName = e.FkUserNavigation.Username }); return(Ok(entries.ToList())); } } }
public ActionResult Patch(int id, [FromBody] Delta <Games> editedGame) { // Get user id from jwt var claims = HttpContext.User.Claims.ToList(); int authorId = int.Parse(claims[0].Value); using (GameJamDBContext context = new GameJamDBContext()) { // Check if game exists Games origGame = context.Games.Find(id); if (origGame == null) { return(NotFound("Game was not found")); } // Check whether author is indeed this games' author if (origGame.FkUser != authorId) { return(BadRequest("You are not the author of this game")); } // Update game data editedGame.Patch(origGame); context.Entry(origGame).State = EntityState.Modified; context.SaveChanges(); return(Ok("Game updated successfully")); } }
public ActionResult <List <Games> > Get(string platform = null, string genre = null) { using (GameJamDBContext context = new GameJamDBContext()) { var games = context.Games.AsEnumerable(); // Filter by platform if (!string.IsNullOrEmpty(platform)) { games = games.Where((g) => g.Platform .ToLower() .Contains(platform.ToLower())); } // Filter by genre if (!string.IsNullOrEmpty(genre)) { games = games.Where((g) => g.Genre .ToLower() .Contains(genre.ToLower())); } // Return list of games if (!games.Any()) { return(NoContent()); } return(Ok(games.ToList())); } }
public ActionResult <List <RatingModel> > GetGameRatings(int id) { using (GameJamDBContext context = new GameJamDBContext()) { // Get ratings list var result = context .Ratings .Include(r => r.FkUserNavigation) .Where(rating => rating.FkGame == id) .Select(r => new RatingModel() { IdUser = r.FkUser, Username = r.FkUserNavigation.Username, UserComment = r.UserComment, FunFactor = r.FunFactor, Graphics = r.Graphics, ThemeAccuracy = r.ThemeAccuracy, Innovation = r.Innovation }); if (!result.Any()) { return(NoContent()); } return(Ok(result.ToList())); } }
public ActionResult Post([FromBody] Games game) { var claims = HttpContext.User.Claims.ToList(); int authorId = int.Parse(claims[0].Value); using (GameJamDBContext context = new GameJamDBContext()) { Users user = context.Users.Find(authorId); if (user == null) { return(BadRequest("No such user exist in the database")); } if (game.FkUser != authorId) { return(BadRequest("User is not the author of the game")); } if (string.IsNullOrEmpty(game.Title) || string.IsNullOrEmpty(game.Description)) { return(BadRequest("Cannot submit empty game entry")); } // Insert new entry to the database context.Games.Add(game); context.SaveChanges(); return(Ok("Game inserted successfully")); } }
public ActionResult <Themes> GetJamTheme(int id) { using (GameJamDBContext context = new GameJamDBContext()) { if (context.Jams.Find(id) == null) { return(BadRequest("Jam was not found")); } // Sort themes by ups/downs var sorted = context.Themes .Where((t) => t.FkJam == id) .OrderByDescending((t) => t.Upvotes - t.Downvotes); if (!sorted.Any()) { return(Ok(new Themes() { Title = "None" })); } else { Themes theme = sorted.First(); return(Ok(theme)); } } }
public ActionResult <GameAuthor> GetGameAuthor(int id) { using (GameJamDBContext context = new GameJamDBContext()) { Games game = context.Games.Find(id); if (game == null) { return(BadRequest("Author of this game was not found")); } var author = context .Users .Where((u) => u.IdUser == game.FkUser) .Select(u => new GameAuthor() { Username = u.Username, JoinedDate = u.JoinedDate }) .First(); if (author == null) { return(BadRequest("Author of this game does not exist")); } return(Ok(author)); } }
public ActionResult Patch(int id, [FromBody] Delta <Users> editedUser) { var claims = HttpContext.User.Claims.ToList(); int userId = int.Parse(claims[0].Value); using (GameJamDBContext context = new GameJamDBContext()) { if (userId != id) { return(BadRequest("User can only edit his own profile")); } Users origUser = context.Users.Find(id); if (origUser == null) { return(BadRequest("User was not found")); } // Patch user information editedUser.Patch(origUser); context.Entry(origUser).State = EntityState.Modified; context.SaveChanges(); return(Ok("User information updated")); } }
public ActionResult <UserInfo> GetUser(int id) { using (GameJamDBContext context = new GameJamDBContext()) { Users user = context.Users.Find(id); if (user == null) { return(BadRequest("User was not found")); } return(Ok((UserInfo)user)); } }
public ActionResult <List <Jams> > Get() { using (GameJamDBContext context = new GameJamDBContext()) { var jams = context.Jams; if (!jams.Any()) { return(NoContent()); } return(Ok(jams.ToList())); } }
public ActionResult <Jams> Get(int id) { using (GameJamDBContext context = new GameJamDBContext()) { Jams jam = context.Jams.Find(id); if (jam == null) { return(BadRequest("Jam was not found")); } return(Ok(jam)); } }
public ActionResult <IEnumerable <Games> > GetUserGames(int id) { using (GameJamDBContext context = new GameJamDBContext()) { var games = context.Games.Where((g) => g.FkUser == id); if (!games.Any()) { return(NoContent()); } return(Ok(games.ToList())); } }
public ActionResult <Games> GetGame(int id) { using (GameJamDBContext context = new GameJamDBContext()) { Games game = context .Games .Find(id); if (game == null) { return(BadRequest("Game was not found")); } return(Ok(game)); } }
private Users CheckUser(string username, string password) { Users user = null; using (GameJamDBContext context = new GameJamDBContext()) { string pass = ComputeSha256(password); user = context.Users.FirstOrDefault(u => u.Username == username && u.Password == pass); if (user == null) { return(null); } } return(user); }
public ActionResult Delete(int id) { using (GameJamDBContext context = new GameJamDBContext()) { Users user = context.Users.Find(id); if (user == null) { return(BadRequest("User with id was not found")); } context.Users.Remove(user); context.SaveChanges(); return(Ok("User deleted successfully")); } }
public ActionResult Delete(int id) { using (GameJamDBContext context = new GameJamDBContext()) { Jams jam = context.Jams.Find(id); if (jam == null) { return(BadRequest("Jam you're trying to delete was not found")); } //TODO: check if other related entities needs deleting context.Jams.Remove(jam); context.SaveChanges(); return(Ok("Jam deleted successfully")); } }
public ActionResult Patch(int id, [FromBody] Delta <Jams> editedJam) { using (GameJamDBContext context = new GameJamDBContext()) { Jams origJam = context.Jams.Find(id); if (origJam == null) { return(BadRequest("Jam was not found")); } editedJam.Patch(origJam); context.Entry(origJam).State = EntityState.Modified; context.SaveChanges(); return(Ok("Jam updated successfully")); } }
public ActionResult <List <UserInfo> > Get(int role = 1) { using (GameJamDBContext context = new GameJamDBContext()) { var users = context.Users.Where(u => u.Role == role); if (!users.Any()) { return(NoContent()); } List <UserInfo> userList = new List <UserInfo>(); foreach (Users u in users) { userList.Add(u); } return(Ok(userList)); } }
public ActionResult <int> Post([FromBody] Jams jam) { using (GameJamDBContext context = new GameJamDBContext()) { if (string.IsNullOrEmpty(jam.Title) || string.IsNullOrEmpty(jam.Description)) { return(BadRequest("Title and/or description can't be empty")); } context.Jams.Add(jam); int added = context.SaveChanges(); if (added < 1) { return(BadRequest("Could not create jam")); } return(Ok(jam.IdJam));//Ok("Jam created successfully"); } }
public ActionResult Post([FromBody] Users newUser) { using (GameJamDBContext context = new GameJamDBContext()) { Users user = context.Users.FirstOrDefault(u => u.Username == newUser.Username); if (user != null) { return(BadRequest("User with this name already exists")); } newUser.JoinedDate = DateTime.Now; context.Users.Add(newUser); int added = context.SaveChanges(); if (added < 1) { return(BadRequest("Error while inserting user")); } return(Ok("User created successfully")); } }
public ActionResult GetJamWinner(int id, string category = null) { using (GameJamDBContext context = new GameJamDBContext()) { JamWinners winners = new JamWinners(); var entries = context .Games .Where((g) => g.FkJam == id) .Include(g => g.Ratings) .AsEnumerable(); if (!entries.Any()) { return(NoContent()); } int count = entries.Count(); // Winner in specific category winners.InFun = entries .OrderByDescending((g) => g.Ratings.Sum((r) => r.FunFactor) / (float)count) .ToList() .First() .IdGame; winners.InTheme = entries .OrderByDescending((g) => g.Ratings.Sum((r) => r.ThemeAccuracy) / (float)count) .ToList() .First() .IdGame; winners.InGraphics = entries .OrderByDescending((g) => g.Ratings.Sum((r) => r.Graphics) / (float)count) .ToList() .First() .IdGame; winners.InInnovation = entries .OrderByDescending((g) => g.Ratings.Sum((r) => r.Innovation) / (float)count) .ToList() .First() .IdGame; if (category != null) { switch (category.ToLower()) { case "fun": return(Ok(winners.InFun)); case "theme": return(Ok(winners.InTheme)); case "graphics": return(Ok(winners.InGraphics)); case "innovation": return(Ok(winners.InInnovation)); } } else { // Overall game jam winner var gamesList = entries .OrderByDescending((g) => { return(g.Ratings.Sum((r) => ((float)r.FunFactor / count) + ((float)r.ThemeAccuracy / count) + ((float)r.Graphics / count) + ((float)r.Innovation) / count)); }) .ToList(); winners.Overall = gamesList.First().IdGame; } if (winners == null) { return(NoContent()); } else { return(Ok(winners)); } } }