// adds a Demo User (CoUser)to database and returns it private static Result GenerateDemoUser() { Result result = new Result(); using (ForGoodEntities db = new ForGoodEntities()) { string email = Guid.NewGuid().ToString(); while(db.Users.Any(u => u.Email == email)){ email = Guid.NewGuid().ToString(); } email = "demo_" + email + "@forgood.com"; User demoUser = new User() { Email = email, Password = CalculateSHA1("1111"), CreationDateUtc = DateTime.Now.ToUniversalTime() }; db.AddToUsers(demoUser); db.SaveChanges(); result.Code = RESULT_CODES.Succeeded; result.ReturnedObj = MapToCustomObject(demoUser) as CoUser; AddCard(email, "What is the supreme law of the land?", "The Constitution"); AddCard(email, "What does the Constitution do?", "sets up the government\ndefines the government\nprotects basic rights of Americans"); } return result; }
// returns CardId (int) private static Result AddCard(string email, string text, string backText) { Result result = new Result(); using (ForGoodEntities db = new ForGoodEntities()) { User user = db.Users.First(u => u.Email == email); Card card = new Card() { Text = text, BackText = backText, CreationDateUtc = DateTime.UtcNow, DueDateUtc = DateTime.UtcNow, User = user }; db.AddToCards(card); db.SaveChanges(); result.Code = RESULT_CODES.Succeeded; result.ReturnedObj = card.CardId; } return result; }
// return nothing private static Result SetNewReviewTime(string email, int cardId, string duration) { Result result = new Result(); Random ran = new Random(); DateTime newDue = DateTime.UtcNow; switch (duration.ToLower()) { case REVIEW_DURATIONS.Hour: newDue = newDue.AddHours(ran.Next(1, 24)); break; case REVIEW_DURATIONS.Day: newDue = newDue.AddDays(ran.Next(1, 7)); break; case REVIEW_DURATIONS.Week: newDue = newDue.AddDays(ran.Next(1, 7) * 7); break; case REVIEW_DURATIONS.Month: newDue = newDue.AddMonths(ran.Next(1, 12)); break; default: result.Code = RESULT_CODES.Failed; result.ErrorCodes = new List<int>() { ERROR_CODES.SetNewReviewTime_InvalidReviewDuration }; return result; } using (ForGoodEntities db = new ForGoodEntities()) { var query = db.Cards.Where(c => c.User.Email == email && c.CardId == cardId); if (query.Count() != 1) { result.Code = RESULT_CODES.Failed; result.ErrorCodes = new List<int>() { ERROR_CODES.SetNewReviewTime_CardNotExist }; } else { Card card = query.First(); card.DueDateUtc = newDue; db.SaveChanges(); result.Code = RESULT_CODES.Succeeded; } } return result; }
// returns UserId (int) private static Result RegNewUser(string email, string password) { Result result = new Result(); using (ForGoodEntities db = new ForGoodEntities()) { if (db.Users.Any(u => u.Email == email)) { result.Code = RESULT_CODES.Failed; result.ErrorCodes.Add(ERROR_CODES.RegNewUser_EmailAlreadyExists); } else { User user = new User() { Email = email, Password = password, CreationDateUtc = DateTime.Now.ToUniversalTime() }; db.AddToUsers(user); db.SaveChanges(); result.Code = RESULT_CODES.Succeeded; result.ReturnedObj = user.UserId; } } return result; }