public async Task <HttpResponseMessage> GetLearningActivity(int id)
        {
            Common.Models.LearningActivity limitedVersion = GetAllActivitiesWhere(a => a.Id == id).FirstOrDefault();
            if (limitedVersion == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not found"));
            }

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

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

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

            return(resp);
        }
        public async Task <HttpResponseMessage> GetWithCode(string code)
        {
            if (code == "SALTWELLSTATUE")
            {
                code = "CHARLTONSTATUE";
            }

            Common.Models.LearningActivity limitedVersion = GetAllActivitiesWhere(a => a.InviteCode == code.ToUpper()).FirstOrDefault();
            if (limitedVersion == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.NotFound, "Not found"));
            }

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

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

            await MakeLog(new Dictionary <string, string>() { { "code", code } });

            return(resp);
        }
        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);
        }