示例#1
0
        public IHttpActionResult Create(Journey journey)
        {
            Trace.WriteLine("Receive create journey: " + JsonConvert.SerializeObject(journey));
            Reply  reply = new Reply();
            String json  = "";

            if (ModelState.IsValid)
            {
                journey.Status = "Started";
                var userID = User.Identity.GetUserId();

                journey.UserProfileId = userID;
                journey.StartTime     = DateTime.Now;
                journey.EndTime       = null;
                Trace.WriteLine("Write to database: " + JsonConvert.SerializeObject(journey));
                journey = db.Journey.Add(journey);
                Trace.WriteLine("Add finish, journey ID: " + journey.JourneyId);
                //Create Templink;
                string checkcode = this.generateTempLink();

                //if the checkcode already exist in database, re-generate one if exist
                while (db.TempLink.Find(checkcode) != null)
                {
                    checkcode = this.generateTempLink();
                }

                List <TempLink> availiableLinks = db.TempLink.Where(s => s.UserProfileId == userID).ToList();
                foreach (TempLink tempLink in availiableLinks)
                {
                    db.TempLink.Remove(tempLink);
                }

                TempLink theTemp = new TempLink();
                theTemp.TempLinkId       = checkcode;
                theTemp.JourneyJourneyId = journey.JourneyId;;
                theTemp.UserProfileId    = journey.UserProfileId;
                db.TempLink.Add(theTemp);

                JTracking newTrack = new JTracking();
                newTrack.JourneyJourneyId = journey.JourneyId;
                newTrack.Time             = DateTime.Now;
                newTrack.CoordLat         = journey.SCoordLat;
                newTrack.CoordLog         = journey.SCoordLog;
                db.JTracking.Add(newTrack);

                UsefulFunction.dbSave(db);

                JourneyCreateReplyData data = new JourneyCreateReplyData();
                data.journeyID  = journey.JourneyId;
                data.TempLinkId = theTemp.TempLinkId;
                reply.result    = "success";
                reply.data      = JsonConvert.SerializeObject(data);
                json            = JsonConvert.SerializeObject(reply);
                return(Ok(json));
            }
            reply.result = "failed";
            reply.errors = "data not match";
            json         = JsonConvert.SerializeObject(reply);
            return(BadRequest(json));
        }
示例#2
0
        public IHttpActionResult journeyFinish(journeyFinishModel finishModel)
        {
            Trace.WriteLine("Receive create journey: " + JsonConvert.SerializeObject(finishModel));
            Reply  reply = new Reply();
            String json  = "";

            if (User.Identity.IsAuthenticated)
            {
                var     userID     = User.Identity.GetUserId(); //get user ID  
                Journey theJourney = db.Journey.Find(finishModel.JourneyId);
                if (theJourney == null)
                {
                    return(BadRequest("Journey ID do not exist"));
                }
                if (userID == theJourney.UserProfileId)
                {
                    theJourney.EndTime   = DateTime.Now;
                    theJourney.ECoordLat = finishModel.ECoordLat;
                    theJourney.ECoordLog = finishModel.ECoordLog;
                    theJourney.Status    = "Finished";
                    //delete templink
                    List <TempLink> theTemp = theJourney.TempLink.ToList();
                    Trace.WriteLine("find temp links: " + theTemp.Count());
                    //db.TempLink.Where(s=>s.JourneyJourneyId == theJourney.JourneyId).ToList();
                    foreach (TempLink temp in theTemp)
                    {
                        db.TempLink.Remove(temp);
                    }

                    db.Entry(theJourney).State = EntityState.Modified;
                    UsefulFunction.dbSave(db);
                    reply.result = "success";
                    json         = JsonConvert.SerializeObject(reply);
                    return(Ok(json));
                }
            }
            reply.result = "failed";
            reply.errors = "data not match";
            json         = JsonConvert.SerializeObject(reply);
            return(BadRequest(json));
        }
示例#3
0
        public IHttpActionResult Create(JTracking jTracking)
        {
            Trace.WriteLine("Receive create JTracking: " + JsonConvert.SerializeObject(jTracking));

            Reply  reply  = new Reply();
            String json   = "";
            var    UserID = User.Identity.GetUserId();

            if (ModelState.IsValid)
            {
                JTracking newTrking  = new JTracking();
                Journey   theJourney = db.Journey.Find(jTracking.JourneyJourneyId);
                if (theJourney == null)
                {
                    return(BadRequest("journeyId not exist"));
                }
                if (UserID != theJourney.UserProfileId)
                {
                    return(BadRequest("You are not authorized to chance this journey ID"));
                }
                newTrking.JourneyJourneyId = jTracking.JourneyJourneyId;
                newTrking.Time             = DateTime.Now;
                newTrking.CoordLat         = jTracking.CoordLat;
                newTrking.CoordLog         = jTracking.CoordLog;

                Trace.WriteLine("Write to JTracking: " + JsonConvert.SerializeObject(newTrking));
                db.JTracking.Add(newTrking);
                UsefulFunction.dbSave(db);

                reply.result = "success";
                json         = JsonConvert.SerializeObject(reply);
                return(Ok(json));
            }

            //ViewBag.JourneyJourneyId = new SelectList(db.Journey, "JourneyId", "EndTime", jTracking.JourneyJourneyId);
            reply.result = "failed";
            json         = JsonConvert.SerializeObject(reply);
            return(BadRequest(json));
        }