public static File GetFile(long userId) { using (var dbContext = new PlannerDbContext()) { dbContext.Configuration.ProxyCreationEnabled = false; return dbContext.Set<File>().FirstOrDefault(_ => _.UserId == userId); } }
public static string Login(string email, string password) { Logger.log("Login now"); using (var dbContext = new PlannerDbContext()) { var user = dbContext .Set<User>() .FirstOrDefault(_ => _.Email.Equals(email) && _.Password.Equals(password)); if (user == null) { return (-1).ToString(); } return user.Id + ", " + user.Type; } }
public static List<Shake> getAllInTimeRange(DateTime start, DateTime end, long userId) { using (var dbContext = new PlannerDbContext()) { dbContext.Configuration.ProxyCreationEnabled = false; var query = from _ in dbContext.Shakes where (_.ShakeTime >= start) where (_.ShakeTime <= end) where (_.Status == "OPEN") where (_.UserId != userId) orderby (_.ShakeTime) select _; return query.ToList(); } }
public static long log(string message) { using (var dbContext = new PlannerDbContext()) { var log = new Log { Message = message, Datetime = DateTime.Now }; dbContext.Logs.Add(log); try { dbContext.SaveChanges(); return log.Id; } catch (DbEntityValidationException exception) { return -1; } } }
public static bool Add(string fileAsString, long userId) { using (var dbContext = new PlannerDbContext()) { var file = new File { FileLink = fileAsString, UserId = userId }; dbContext.Files.Add(file); try { dbContext.SaveChanges(); return true; } catch (DbEntityValidationException exception) { return false; } } }
public static DateTime recordShake(long userId) { var now = DateTime.Now; using (var dbContext = new PlannerDbContext()) { var shake = new Shake { UserId = userId, ShakeTime = now, Status = "OPEN", }; dbContext.Shakes.Add(shake); try { dbContext.SaveChanges(); return now; } catch (DbEntityValidationException exception) { return "0001-01-01".AsDateTime(); } } }
public static long Create(string email, string password, string type, string tagline, string fullName) { using (var dbContext = new PlannerDbContext()) { var user = new User { Email = email, Password = password, Type = type, Tagline = tagline, FullName = fullName }; dbContext.Users.Add(user); try { dbContext.SaveChanges(); return user.Id; } catch (DbEntityValidationException exception) { var s = ""; foreach (var eve in exception.EntityValidationErrors) { s += string.Format( "Entity of type \"{0}\" in state \"{1}\" has the following validation errors:", eve.Entry.Entity.GetType().Name, eve.Entry.State); foreach (var ve in eve.ValidationErrors) { s += string.Format("- Property: \"{0}\", Error: \"{1}\"", ve.PropertyName, ve.ErrorMessage); } } return -1; } } }
public static bool makeConnection(long one, long two) { //setAsPaired(one); //setAsPaired(two); using (var dbContext = new PlannerDbContext()) { var connection = new Connection { UserA = one, UserB = two }; dbContext.Connections.Add(connection); try { dbContext.SaveChanges(); return true; } catch (DbEntityValidationException exception) { return false; } } }
public static IEnumerable<Handshake> GetOwnedHandshakesFromSender(long userId) { using (var dbContext = new PlannerDbContext()) { dbContext.Configuration.ProxyCreationEnabled = false; var query = from _ in dbContext.Handshakes where (_.SecondaryUserId == userId) select _; return query.ToList(); } }
public static IEnumerable<Connection> GetConnections(long userId) { using (var dbContext = new PlannerDbContext()) { dbContext.Configuration.ProxyCreationEnabled = false; var query = from _ in dbContext.Connections where (_.UserA == userId) where (_.UserB != userId) select _; return query.ToList(); } }
public static void setAsPaired(long userId) { using (var dbContext = new PlannerDbContext()) { dbContext.Configuration.ProxyCreationEnabled = false; var query = from _ in dbContext.Shakes where (_.Status == "OPEN") where (_.UserId != userId) orderby (_.ShakeTime) select _; var list = query.ToList(); var toSetAsPaired = list[list.Count - 1]; var newShake = new Shake { Id = toSetAsPaired.Id, Location = toSetAsPaired.Location, UserId = userId, ShakeTime = toSetAsPaired.ShakeTime, Status = "PAIRED" }; dbContext.Shakes.Attach(newShake); dbContext.Entry(newShake).State = EntityState.Modified; dbContext.SaveChanges(); } }