コード例 #1
0
        public IActionResult New(TRApplication model)
        {
            ViewData["Title"] = "New application";

            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            model.AdminKey  = Guid.NewGuid();
            model.PublicKey = Guid.NewGuid();
            model.UpdateKey = Guid.NewGuid();

            using (var trContext = new TRContext())
            {
                trContext.Applications.Add(model);
                trContext.SaveChanges();
            }

            using (var context = new ApplicationContext(model.PublicKey))
            {
                context.Database.EnsureCreated();
            }

            return(RedirectToAction("Admin", new { adminKey = model.AdminKey }));
        }
コード例 #2
0
        public IActionResult Update(Guid updateKey)
        {
            try
            {
                TRApplication trApp = null;
                using (var trContext = new TRContext())
                {
                    trApp = trContext.Applications.Where(q => q.UpdateKey == updateKey).FirstOrDefault() ?? throw new Exception("No such app");
                }

                using (var context = new ApplicationContext(trApp.PublicKey))
                {
                    var d = new Data();

                    if (Request.Query.ContainsKey("timestamp") &&
                        long.TryParse(Request.Query["timestamp"][0], out long ts) &&
                        ts < (CurrentEpoch + 60000))
                    {
                        d.InsertDate = new DateTime(1970, 1, 1).AddMilliseconds(ts);
                    }
                    else
                    {
                        d.InsertDate = DateTime.Now;
                    }

                    if (Request.Method == "GET")
                    {
                        foreach (var item in Request.Query)
                        {
                            if (item.Key != "timestamp")
                            {
                                var di = new DataItem()
                                {
                                    PropertyName  = item.Key,
                                    PropertyValue = item.Value[0]
                                };
                                d.DataItems.Add(di);
                            }
                        }
                    }

                    if (Request.Method == "POST")
                    {
                        foreach (var item in Request.Form)
                        {
                            if (item.Key != "timestamp")
                            {
                                var di = new DataItem()
                                {
                                    PropertyName  = item.Key,
                                    PropertyValue = item.Value[0]
                                };
                                d.DataItems.Add(di);
                            }
                        }
                    }

                    context.Data.Add(d);

                    context.SaveChanges();
                }

                using (var trContext = new TRContext())
                {
                    trContext.Applications.Where(q => q.UpdateKey == updateKey).FirstOrDefault().LastUpdated = DateTime.Now;
                    trContext.SaveChanges();
                }
            }
            catch (Exception e)
            {
                return(Json(new { success = false, message = e.Message }));
            }

            return(Json(true));
        }