public bool addUsersToRoom(ICollection<User> usersId, int idRoom) { using (DatabaseContext db = new DatabaseContext()) { try { var room = (from r in db.GameRooms where r.Id == idRoom select r).First(); ICollection<User> users = new List<User>(); foreach(var us in usersId) { var nesto = (from u in db.Users where u.Id == us.Id select u).First(); users.Add(nesto); } room.Users = users; db.SaveChanges(); return true; } catch (Exception e) { return false; } } }
public bool changePassword(int idUser, string newpass) { using(DatabaseContext db = new DatabaseContext()) { try { var user = (from u in db.Users where u.Id == idUser select u).First(); user.Password = newpass; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return true; } catch(Exception e) { return false; } } }
private ServerContext() { gameRooms = new List<GameRoom>(); idGenerator = new Random(); roomServices = new RoomServices(); userServices = new UserServices(); databaseContext = new DatabaseContext(); }
public bool addRoom(GameRoom room) { using (DatabaseContext db = new DatabaseContext()) { try { if (getRoom(room.Id) != null) return false; else { db.GameRooms.Add(room); db.SaveChanges(); return true; } } catch (Exception e) { return false; } } }
public bool deleteUser(int idUser) { using (DatabaseContext db = new DatabaseContext()) { try { var user = db.Users.Find(idUser); if (user == null) return false; db.Users.Remove(user); db.SaveChanges(); return true; } catch (Exception e) { return false; } } }
public bool deleteRoom(int idRoom) { using (DatabaseContext db = new DatabaseContext()) { try { var room = db.GameRooms.Find(idRoom); if (room == null) return false; db.GameRooms.Remove(room); db.SaveChanges(); return true; } catch (Exception e) { return false; } } }
public bool addUser(User user) { using (DatabaseContext db = new DatabaseContext()) { try { if (getUser(user) != null) return false; else { User u = new User() { Username = user.Username, Password = user.Password, Role = "User" }; db.Users.Add(u); db.SaveChanges(); return true; } } catch (Exception e) { return false; } } }
public bool makeWinner(int idUser, int idGameRoom) { using (DatabaseContext db = new DatabaseContext()) { try { var gr = (from g in db.GameRooms where g.Id == idGameRoom select g).First(); gr.WinnerId = idUser; db.SaveChanges(); return true; } catch (Exception e) { return false; } } }
public ICollection<User> getUsersInRoom(int idRoom) { using (DatabaseContext db = new DatabaseContext()) { try { var users = (from r in db.GameRooms where r.Id == idRoom select r).Include("Users").First().Users.ToList(); return users; } catch (Exception e) { return null ; } } }
public IEnumerable<GameRoom> getRooms() { using(DatabaseContext db = new DatabaseContext()) { return db.GameRooms.Include("Users").ToList().OrderByDescending(x => x.Date); } }
public GameRoom getRoom(int idRoom) { using (DatabaseContext db = new DatabaseContext()) { try { var room = (from r in db.GameRooms where r.Id == idRoom select r).Include("Users").First(); return room; } catch (Exception e) { return null; } } }
public IEnumerable<GameRoom> getRoomsForUser(int idUser) { using (DatabaseContext db = new DatabaseContext()) { try { List<GameRoom> gamerooms = new List<GameRoom>(); var rooms = (from r in db.GameRooms select r).Include("Users").ToList(); foreach(var room in rooms) { foreach(var user in room.Users.ToList()) { if (user.Id == idUser) { gamerooms.Add(room); break; } } } // TODO: Return rooms for one user return gamerooms; } catch (Exception e) { return null; } } }
public bool updateUserRole(int idUser, string Role) { using (DatabaseContext db = new DatabaseContext()) { try { User user = (from us in db.Users where us.Id == idUser select us).First(); user.Role = Role; db.Entry(user).State = EntityState.Modified; db.SaveChanges(); return true; } catch (Exception e) { return false; } } }
public IEnumerable<User> getUsers() { using (DatabaseContext db = new DatabaseContext()) { try { return db.Users.ToList(); } catch(Exception e) { return null; } } }
public string getUserRole(int idUser) { using(DatabaseContext db = new DatabaseContext()) { try { var userRole = from u in db.Users where u.Id == idUser select u.Role; if (userRole.Count() != 1) return "None"; return userRole.First(); } catch { return "Error"; } } }
public User getUser(User user) { using (DatabaseContext db = new DatabaseContext()) { try { var us = (from u in db.Users where u.Username == user.Username && u.Password == user.Password select u); if (us.Count() != 1) return null; else return us.First(); } catch (Exception e) { return null; } } }