/// <summary> /// Add a new tracking information(Location of animals when animal moves) /// </summary> /// <param name="gpsLocationInfo">The GPS location info</param> /// <returns>The details of gps tracking info</returns> public GPSTrackingInfo AddNewTrackingDetails(GPSTrackingInfo gpsLocationInfo) { log.Info("Adding a new tracking info : AddNewTrackingDetails with GPS device ID : " + gpsLocationInfo.gpsDeviceId); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { //Fetches the animal for given device Id var animal = (from p in dbContext.tblanimals where p.gpsDeviceId == gpsLocationInfo.gpsDeviceId select p).FirstOrDefault(); gpsLocationInfo.animalId = animal.animalId; tblgpstracking trackingEntity = JsonConvert.DeserializeObject <tblgpstracking>(JsonConvert.SerializeObject(gpsLocationInfo)); trackingEntity.createdAt = DateTime.Now; //Adds the details to the DB dbContext.tblgpstrackings.Add(trackingEntity); try { // Saves the details in DB dbContext.SaveChanges(); if (trackingEntity != null) { gpsLocationInfo.latitude = trackingEntity.latitude; gpsLocationInfo.longitude = trackingEntity.longitude; gpsLocationInfo.animalId = trackingEntity.animalId; gpsLocationInfo.createdAt = trackingEntity.createdAt; gpsLocationInfo.trackingId = trackingEntity.trackingId; } log.Info("The GPS location is successfully saved in the DB."); return(gpsLocationInfo); } //Saves the entries that could not be saved into the DB. It resolves the concurrency exception with Reload catch (DbUpdateConcurrencyException Ex) { Ex.Entries.Single().Reload(); dbContext.SaveChanges(); gpsLocationInfo.latitude = trackingEntity.latitude; gpsLocationInfo.longitude = trackingEntity.longitude; gpsLocationInfo.animalId = trackingEntity.animalId; gpsLocationInfo.createdAt = trackingEntity.createdAt; gpsLocationInfo.trackingId = trackingEntity.trackingId; log.Info("The GPS location is successfully saved in the DB."); return(gpsLocationInfo); } catch (Exception e) { log.Error("Error in adding the animal : " + e.StackTrace); ErrorHandler error = new ErrorHandler("Error", e.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } } }
/// <summary> /// Add the tracking info (Location of animal) /// </summary> /// <param name="animalDetails">The animal details</param> /// <returns>The details of animal that is added</returns> public GPSTrackingInfo AddTracking(GPSTrackingInfo gpsDetails) { return(trackingRepo.AddNewTrackingDetails(gpsDetails)); }