internal static void Logic(HttpContext context, IEnsureClientController controller)
        {
            Guid clientid = Guid.Empty;
            var cookie = context.Request.Cookies["ClientID"];

            if (cookie != null)
            {
                Guid.TryParse(cookie.Value, out clientid);
            }

            if (clientid == Guid.Empty)
            {
                clientid = Guid.NewGuid();
            }

            cookie = new HttpCookie("ClientID", clientid.ToString());
            cookie.Domain = context.Request.Url.Host;
            cookie.Expires = DateTime.Now.AddYears(1);
            cookie.HttpOnly = true;
            context.Response.SetCookie(cookie);

            using (var db = new Context())
            {
                var client = db.Clients.Find(clientid);

                if (client == null)
                {
                    client = new Client
                    {
                        ClientID = clientid,
                        Name = "Unnamed"
                    };

                    db.Clients.Add(client);

                    db.SaveChanges();
                }
            }

            if (controller != null)
            {
                controller.ClientID = clientid;
            }
        }
        public Beacon StoreBeacon(Beacon beacon)
        {
            using (var db = new Context())
            {
                var oldBeacon = db.Beacons.Find(beacon.UUID, beacon.Major, beacon.Minor);

                if (oldBeacon != null)
                {

                    ((IObjectContextAdapter)db).ObjectContext.Detach(oldBeacon);
                    db.Beacons.Attach(beacon);
                    db.Entry(beacon).State = System.Data.Entity.EntityState.Modified;
                }
                else
                {
                    db.Beacons.Add(beacon);
                }

                db.SaveChanges();

                return oldBeacon;
            }
        }
Example #3
0
 public void PutName(SetNameModel model)
 {
     using (var db = new Context())
     {
         var me = db.Clients.Find(ClientID);
         me.Name = model.Name;
         db.SaveChanges();
     }
 }