Exemplo n.º 1
0
        public async Task <HttpResponseMessage> PostActivityCollection(ActivityCollection collection)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid LearningActivity"));
            }

            ApplicationUser thisUser = await GetUser();

            if (thisUser == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Please log in"));
            }

            Application thisApp = db.Applications.AsEnumerable().FirstOrDefault();

            collection.Places = await ProcessPlacesInNewContent(collection.Places, thisUser);

            collection.Activities = await ProcessActivities(collection.Activities, thisUser);

            collection.Author      = thisUser;
            collection.CreatedAt   = DateTime.UtcNow;
            collection.Approved    = thisUser.Trusted;
            collection.Application = thisApp;

            bool   createdUnique = false;
            string randStr       = "";

            while (!createdUnique)
            {
                randStr = new string(Enumerable.Repeat(chars, 6)
                                     .Select(s => s[rand.Next(s.Length)]).ToArray());

                createdUnique = !await db.ActivityCollections.AnyAsync(la => la.InviteCode == randStr);
            }

            // ~ 900k possible combinations. If this becomes an issue we're in a good place :)
            collection.InviteCode = randStr;

            ActivityCollection finalAct = db.ActivityCollections.Add(collection);


            string qrCodeUrl    = "qrCodes/" + finalAct.InviteCode + ".png";
            string shareAddress = ServerUtils.GetActivityShareUrl(finalAct.InviteCode);

            finalAct.QRCodeUrl = await GenerateQR(finalAct.InviteCode, shareAddress);

            await db.SaveChangesAsync();

            await MakeLog(new Dictionary <string, string>() { { "id", finalAct.Id.ToString() } });

            return(Request.CreateResponse(HttpStatusCode.OK));;
        }
        public async Task <HttpResponseMessage> PostLearningActivity(LearningActivity learningActivity)
        {
            if (!ModelState.IsValid)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Invalid LearningActivity"));
            }

            ApplicationUser thisUser = await GetUser();

            if (thisUser == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Please log in"));
            }

            Application thisApp = db.Applications.AsEnumerable().FirstOrDefault();

            learningActivity.Places = await ProcessPlacesInNewContent(learningActivity.Places, thisUser);

            learningActivity.LearningTasks = await ProcessTasks(learningActivity, thisUser);

            learningActivity.Author      = thisUser;
            learningActivity.CreatedAt   = DateTime.UtcNow;
            learningActivity.Approved    = thisUser.Trusted;
            learningActivity.Application = thisApp;

            bool   createdUnique = false;
            string randStr       = "";

            while (!createdUnique)
            {
                randStr = new string(Enumerable.Repeat(chars, 6)
                                     .Select(s => s[rand.Next(s.Length)]).ToArray());

                createdUnique = !await db.LearningActivities.AnyAsync(la => la.InviteCode == randStr);
            }

            // ~ 900k possible combinations. If this becomes an issue we're in a good place :)
            learningActivity.InviteCode = randStr;

            LearningActivity finalAct = db.LearningActivities.Add(learningActivity);

            foreach (Place p in learningActivity.Places)
            {
                p.Activities.Add(finalAct);
            }

            string qrCodeUrl    = "qrCodes/" + finalAct.InviteCode + ".png";
            string shareAddress = ServerUtils.GetActivityShareUrl(finalAct.InviteCode);

            finalAct.QRCodeUrl = await GenerateQR(finalAct.InviteCode, shareAddress);

            await db.SaveChangesAsync();

            Common.Models.LearningActivity limitedVersion = GetAllActivitiesWhere(a => a.Id == finalAct.Id).FirstOrDefault();

            await MakeLog(new Dictionary <string, string>() { { "id", finalAct.Id.ToString() } });

            var resp = Request.CreateResponse(HttpStatusCode.OK);

            resp.Content = new StringContent(JsonConvert.SerializeObject(limitedVersion, new JsonSerializerSettings()
            {
                ReferenceLoopHandling = ReferenceLoopHandling.Ignore
            }), Encoding.UTF8, "application/json");
            return(resp);
        }