Exemplo n.º 1
0
        public async Task<ActionResult> Sync()
        {
            ulong? requestId = null;
            SchedulerSyncRequest syncRequest = null;

            try
            {
                string json = await getPostBody();

                var scheduler = new Scheduler(GetMySQLConnectionString());

                // decode request object
                try
                {
                    syncRequest = JsonConvert.DeserializeObject<SchedulerSyncRequest>(json, new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = dateFormat });
                }
                catch (Exception)
                {
                    throw new Exception("Invalid sync JSON");
                }

                // initialize phantom to real Id maps
                scheduler.InitRowsHolders();

                // get request identifier
                requestId = syncRequest.requestId;

                // initialize response object
                var syncResponse = new SchedulerSyncResponse(requestId);

                // Here we reject client's changes if we suspect that they are out-dated
                // considering difference between server and client revisions.
                // You can get rid of this call if you don't need such behavior.
                scheduler.CheckRevision(syncRequest.revision);

                // if a corresponding store modified data are provided then we handle them

                ResourceSyncHandler resourcesHandler = null;
                if (syncRequest.resources != null)
                {
                    resourcesHandler = new ResourceSyncHandler(scheduler);
                    syncResponse.resources = resourcesHandler.Handle(syncRequest.resources, ResourceSyncHandler.Rows.AddedAndUpdated);
                }
                EventSyncHandler eventsHandler = null;
                if (syncRequest.events != null)
                {
                    eventsHandler = new EventSyncHandler(scheduler, dateFormat);
                    syncResponse.events = eventsHandler.Handle(syncRequest.events, EventSyncHandler.Rows.AddedAndUpdated);
                }

                if (syncRequest.events != null)
                    syncResponse.events = eventsHandler.HandleRemoved(syncRequest.events, syncResponse.events);

                if (syncRequest.resources != null)
                    syncResponse.resources = resourcesHandler.HandleRemoved(syncRequest.resources, syncResponse.resources);

                syncResponse.events = AddModifiedRows(scheduler, "events", syncResponse.events);
                syncResponse.resources = AddModifiedRows(scheduler, "resources", syncResponse.resources);

                // put current server revision to the response
                syncResponse.revision = scheduler.GetRevision();

                scheduler.context.SaveChanges();

                return Content(JsonConvert.SerializeObject(syncResponse), "application/json");
            }
            catch (Exception e)
            {
                return Content(JsonConvert.SerializeObject(new ErrorResponse(e, requestId)), "application/json");
            }
        }
        public ActionResult Sync()
        {
            ulong? requestId = null;
            SchedulerSyncRequest<RoomBooking, Room> syncRequest = null;

            try
            {
                string json = getPostBody();

                var scheduler = new DemoScheduler();

                // decode request object
                try
                {
                    syncRequest = JsonConvert.DeserializeObject<SchedulerSyncRequest<RoomBooking, Room>>(json, new Newtonsoft.Json.Converters.IsoDateTimeConverter { DateTimeFormat = dateFormat });
                }
                catch (Exception)
                {
                    throw new Exception("Invalid sync JSON");
                }

                // initialize phantom to real Id maps
                scheduler.InitRowsHolders();

                // get request identifier
                requestId = syncRequest.requestId;

                // initialize response object
                var syncResponse = new SchedulerSyncResponse(requestId);

                // Here we reject client's changes if we suspect that they are out-dated
                // considering difference between server and client revisions.
                // You can get rid of this call if you don't need such behavior.
                scheduler.checkRevision(syncRequest.revision);

                // if a corresponding store modified data are provided then we handle them

                ResourceSyncHandler<RoomBooking, Room> resourcesHandler = null;
                if (syncRequest.resources != null)
                {
                    resourcesHandler = new ResourceSyncHandler<RoomBooking, Room>(scheduler);
                    syncResponse.resources = resourcesHandler.Handle(syncRequest.resources, ResourceSyncHandler<RoomBooking, Room>.Rows.AddedAndUpdated);
                }
                EventSyncHandler<RoomBooking, Room> eventsHandler = null;
                if (syncRequest.events != null)
                {
                    eventsHandler = new EventSyncHandler<RoomBooking, Room>(scheduler, dateFormat);
                    syncResponse.events = eventsHandler.Handle(syncRequest.events, EventSyncHandler<RoomBooking, Room>.Rows.AddedAndUpdated);
                }

                if (syncRequest.events != null)
                    syncResponse.events = eventsHandler.HandleRemoved(syncRequest.events, syncResponse.events);

                if (syncRequest.resources != null)
                    syncResponse.resources = resourcesHandler.HandleRemoved(syncRequest.resources, syncResponse.resources);

                syncResponse.events = AddModifiedRows(scheduler, "events", syncResponse.events);
                syncResponse.resources = AddModifiedRows(scheduler, "resources", syncResponse.resources);

                // put current server revision to the response
                syncResponse.revision = scheduler.getRevision();

                scheduler.context.SaveChanges();

                return Content(JsonConvert.SerializeObject(syncResponse), "application/json");
            }
            catch (Exception e)
            {
                return Content(JsonConvert.SerializeObject(new ErrorResponse(e, requestId)), "application/json");
            }
        }