Esempio n. 1
0
        private static void ExecuteMethod(HttpContext context)
        {
            JArray jsonArray = GetDecryptedJArrayParams(context);
            string methodName = GetValueFromJsonArray(jsonArray, METHOD_PARAMS.Method);
            // Method required login
            if (!METHODS_DONT_REQUIRE_LOGIN.Contains(methodName)) {
                string email = GetValueFromJsonArray(jsonArray, METHOD_PARAMS.Authentication_Email);
                string password = GetValueFromJsonArray(jsonArray, METHOD_PARAMS.Authentication_Password);
                using (ForGoodEntities db = new ForGoodEntities()) {
                    var sql = db.Users.Where(u => u.Email == email && u.Password == password);
                    if (sql.Count() != 1) {
                        context.Response.StatusCode = (int)HttpStatusCode.Forbidden;
                        return;
                    }
                }
            }

            // Prevent cashing
            context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
            context.Response.Cache.SetNoStore();
            context.Response.Cache.SetExpires(DateTime.MinValue);

            // Handle methods
            Result result = new Result();
            switch (methodName) {
                case AVAILABLE_METHODS.Authentication:
                    result = Authentication(
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.Authentication_Email),
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.Authentication_Password));
                    break;
                case AVAILABLE_METHODS.RegNewUser:
                    result = RegNewUser(
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.RegNewUser_Email),
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.RegNewUser_Password));
                    break;
                case AVAILABLE_METHODS.GetNextToDueCard:
                    result = GetNextToDueCard(
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.GetNextToDueCard_Email));
                    break;
                case AVAILABLE_METHODS.SetNewReviewTime:
                    result = SetNewReviewTime(
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.SetNewReviewTime_Email),
                        Int32.Parse(GetValueFromJsonArray(jsonArray, METHOD_PARAMS.SetNewReviewTime_CardId)),
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.SetNewReviewTime_Duration));
                    break;
                case AVAILABLE_METHODS.AddCard:
                    result = AddCard(
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.AddCard_Email),
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.AddCard_Text),
                        GetValueFromJsonArray(jsonArray, METHOD_PARAMS.AddCard_BackText));
                    break;
                case AVAILABLE_METHODS.GenDemoUser:
                    result = GenerateDemoUser();
                    break;
                default:
                    context.Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return;
            }
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
Esempio n. 2
0
        // 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;
        }
Esempio n. 3
0
 // returns UserId (int)
 private static Result Authentication(string email, string password)
 {
     Result result = new Result();
     using (ForGoodEntities db = new ForGoodEntities()) {
         var sql = db.Users.Where(u => u.Email == email && u.Password == password);
         if (sql.Count() != 1) {
             result.Code = RESULT_CODES.Failed;
             result.ErrorCodes.Add(ERROR_CODES.Authentication_EmailPasswordDontMatch);
         }
         else {
             User user = sql.First();
             result.Code = RESULT_CODES.Succeeded;
             result.ReturnedObj = user.UserId;
         }
     }
     return result;
 }
Esempio n. 4
0
 // 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;
 }
Esempio n. 5
0
 // returns a Card (CoCard)
 private static Result GetNextToDueCard(string email)
 {
     Result result = new Result();
     using (ForGoodEntities db = new ForGoodEntities()) {
         var query = db.Cards.Where(c => c.User.Email == email && c.DueDateUtc < DateTime.UtcNow).OrderBy(c => c.DueDateUtc);
         Card card = new Card();
         //CoCard coCard = new CoCard();
         if (query.Count() > 0) {
             card = query.First();
             result.Code = RESULT_CODES.Succeeded;
             result.ReturnedObj = MapToCustomObject(card) as CoCard;
         }
         else {
             result.Code = RESULT_CODES.Failed;
             result.ErrorCodes = new List<int>(){
                 ERROR_CODES.GetNextToDueCard_NoMoreCards
             };
         }
     }
     return result;
 }
Esempio n. 6
0
        // 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;
        }
Esempio n. 7
0
 // 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;
 }
Esempio n. 8
0
 // return total number of to due cards (int)
 private static Result GetTotalToDueCards(HttpContext context)
 {
     Result result = new Result();
     using (ForGoodEntities db = new ForGoodEntities()) {
         int num = db.Cards.Where(c => c.User.Email == context.User.Identity.Name).Count();
         result.Code = RESULT_CODES.Succeeded;
         result.ReturnedObj = num;
     }
     return result;
 }