/// <summary> /// Deletes animal details /// </summary> /// <param name="animalId">The animal Id</param> /// <returns>The details of deleted animal</returns> public Animal DeleteAnimal(string animalId) { log.Info("Delete animals with animal Id " + animalId + " : DeleteAnimal"); Int32 animId = Convert.ToInt32(animalId); Animal deletedAnimal = new Animal(); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { //fetches the animal detail tblanimal animal = (from p in dbContext.tblanimals where p.animalId == animId select p).FirstOrDefault <tblanimal>(); //If there is an animal for given animalId if (animal == null) { ErrorHandler error = new ErrorHandler("Error Info", "There is no such Animal"); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } //deletes the animal dbContext.tblanimals.Remove(animal); dbContext.SaveChanges(); deletedAnimal = JsonConvert.DeserializeObject <Animal>(JsonConvert.SerializeObject(animal)); log.Info("Successfully deleted the animal with animal Id :" + animal.animalId); return(deletedAnimal); } catch (Exception ex) { log.Error("Error in deleting the animals: " + ex.StackTrace); ErrorHandler error = new ErrorHandler("Error Info", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } } }
/// <summary> /// Updates an animal details /// </summary> /// <param name="animalDetails">The animal details to be deleted</param> /// <returns>The details of updated animal</returns> public Animal UpdateAnimal(Animal animalDetails) { log.Info("Updates animals details : UpdateAnimal"); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { //fetches the animal details. tblanimal animal = (from c in dbContext.tblanimals where c.animalId == animalDetails.animalId select c).FirstOrDefault <tblanimal>(); //if there is such an entry for the animal if (animal == null) { log.Debug("There is no such animal"); //if there is no such an entry for the animal ErrorHandler error = new ErrorHandler("Error Info", "There is no such Animal"); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.NotFound); } animal.animalName = animalDetails.animalName; animal.animalId = animalDetails.animalId; animal.gpsDeviceId = animalDetails.gpsDeviceId; dbContext.SaveChanges(); log.Info("Successfully updated the animal with animal Id :" + animal.animalId); return(animalDetails); } catch (Exception ex) { log.Error("Error in updating the animals: " + ex.StackTrace); ErrorHandler error = new ErrorHandler("Error Info", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } } }
/// <summary> /// This method is used to retrieve all the categories from the database. /// </summary> /// <returns>The list of categories fetched from the DB</returns> public List <Category> RetrieveAllCategories() { log.Info("Retrieve all the categories : RetrieveAllCategories"); List <Category> categoryList = new List <Category>(); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { //Fetches the category details from DB var categories = from p in dbContext.tblcategories select p; foreach (tblcategory category in categories) { Category categoryDetails = JsonConvert.DeserializeObject <Category>(JsonConvert.SerializeObject(category)); log.Debug("The category is" + categoryDetails.categoryName); categoryList.Add(categoryDetails); } log.Info("Successfully retrieved the category details"); return(categoryList); } catch (Exception ex) { log.Error("Error in retrieving the categories: " + ex.StackTrace); ErrorHandler error = new ErrorHandler("Error Info", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.NotFound); } } }
/// <summary> /// Retrieves animal details per category /// </summary> /// <param name="categoryId">The categoryId</param> /// <returns>The list of Animals</returns> public List <Animal> RetrieveAnimalDetailsPerCategory(string categoryId) { log.Info("Retrieve animals with category Id " + categoryId + " : RetrieveAnimalDetailsPerCategory"); Int32 catId = Convert.ToInt32(categoryId); List <Animal> animalList = new List <Animal>(); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { // string sqlQuery = String.Format("SELECT colorIndication, gpsDeviceId, animalId, animalName, categoryName from tblcategory JOIN tblanimal ON tblcategory.categoryId = tblanimal.categoryId where tblanimal.categoryId = '{0}'",catId); //dbContext.Database.SqlQuery<Animal>(sqlQuery).ToList<AnimalCategory>(); //fetaches the animal details var animals = from p in dbContext.tblanimals where p.categoryId == catId select p; try { foreach (tblanimal animal in animals) { Animal animalDetails = JsonConvert.DeserializeObject <Animal>(JsonConvert.SerializeObject(animal)); log.Debug("The animals are" + animalDetails.animalName); animalList.Add(animalDetails); } log.Info("Successfully retrieved the animal details"); return(animalList); } catch (Exception ex) { log.Error("error in retrieving the animals: " + ex.StackTrace); ErrorHandler error = new ErrorHandler("Error Info", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } } }
/// <summary> /// This method is used to update the category detail for the specified categoryId. /// </summary> /// <param name="categoryId">The category id for which the details need to be fetched</param> /// <param name="categoryDetails">The category details that need to be updated</param> /// <returns>The updated category details</returns> public Category UpdateCategory(Category categoryDetails) { log.Info("Update category details for " + categoryDetails.categoryId + " : UpdateCategory"); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { //fetches the category details tblcategory category = (from c in dbContext.tblcategories where c.categoryId == categoryDetails.categoryId select c).FirstOrDefault <tblcategory>(); if (category == null) { ErrorHandler error = new ErrorHandler("Error Info", "There is no such category"); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } category.categoryName = categoryDetails.categoryName; category.categoryDesc = categoryDetails.categoryDesc; category.colorIndication = categoryDetails.colorIndication; dbContext.SaveChanges(); log.Info("Successfully updated category details for category with category id: " + categoryDetails.categoryId); return(categoryDetails); } catch (Exception ex) { log.Error("Error in deleting the category: " + ex.StackTrace); ErrorHandler error = new ErrorHandler("ErrorInfo", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } } }
/// <summary> /// This method is used to delete the category detail for the specified categoryId. /// </summary> /// <param name="categoryId">The category id for which the details need to be fetched</param> /// <returns>The category details for the specified category Id</returns> public Category DeleteCategory(string categoryId) { log.Info("Delete category details for " + categoryId + " : DeleteCategory"); Int32 catId = Convert.ToInt32(categoryId); Category deletedCategory = new Category(); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { //Fetches the animals to see if any animal is associated with the category Int32 categoryCount = (from p in dbContext.tblanimals where p.categoryId == catId select p).Count(); if (!categoryCount.Equals(0) && categoryCount > 0) { ErrorHandler error = new ErrorHandler("Error Info", "Animal is associated with a category"); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } // If no animals are added to the category tblcategory category = (from p in dbContext.tblcategories where p.categoryId == catId select p).FirstOrDefault <tblcategory>(); //Removes the animal from DB dbContext.tblcategories.Remove(category); dbContext.SaveChanges(); deletedCategory = JsonConvert.DeserializeObject <Category>(JsonConvert.SerializeObject(category)); log.Info("Successfully deleted the cateogry with category Id :" + deletedCategory.categoryId); return(deletedCategory); } catch (Exception ex) { log.Error("Error in deleting the animals: " + ex.StackTrace); ErrorHandler error = new ErrorHandler("Error Info", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } } }
/// <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 a new animal /// </summary> /// <param name="animalDetails">The animal details</param> /// <returns>the details of animal </returns> public Animal CreateNewAnimal(Animal animalDetails) { log.Info("Adding a new animal : CreateNewAnimal with GPS device ID" + animalDetails.gpsDeviceId); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { tblanimal animalEntity = JsonConvert.DeserializeObject <tblanimal>(JsonConvert.SerializeObject(animalDetails)); animalEntity.createdAt = DateTime.Now; dbContext.tblanimals.Add(animalEntity); try { //Saves the details of the new animal into database dbContext.SaveChanges(); //If animal is successfully created if (animalEntity != null) { animalDetails.animalId = animalEntity.animalId; animalDetails.categoryId = animalEntity.categoryId; animalDetails.animalName = animalEntity.animalName; } log.Info("The animal is successfully created and saved in the DB."); return(animalDetails); } //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(); //saves the details to DB dbContext.SaveChanges(); animalDetails.animalId = animalEntity.animalId; animalDetails.categoryId = animalEntity.categoryId; animalDetails.animalName = animalEntity.animalName; animalDetails.createdAt = animalEntity.createdAt; log.Info("The animal is successfully created and saved in the DB."); return(animalDetails); } catch (Exception e) { log.Error("The animal creation failed:" + e.StackTrace); ErrorHandler customError = new ErrorHandler("Error Info", e.Message); throw new WebFaultException <ErrorHandler>(customError, HttpStatusCode.BadRequest); } } }
/// <summary> /// This method is used to add a new Category to the database. /// </summary> /// <param name="categoryDetails">The category details to be saved to the DB</param> /// <returns>The success message along with the category details added to the DB</returns> public Category CreateNewCategory(Category categoryDetails) { log.Info("Adding a new category : CreateNewCategory with category name : " + categoryDetails.categoryName); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { tblcategory categoryEntity = JsonConvert.DeserializeObject <tblcategory>(JsonConvert.SerializeObject(categoryDetails)); dbContext.tblcategories.Add(categoryEntity); try { //save the category details in the DB dbContext.SaveChanges(); if (categoryEntity == null) { ErrorHandler customError = new ErrorHandler("Error Info", "Category not created"); throw new WebFaultException <ErrorHandler>(customError, HttpStatusCode.BadRequest); } categoryDetails.categoryId = categoryEntity.categoryId; categoryDetails.categoryDesc = categoryEntity.categoryDesc; log.Info("The category is successfully created and saved in the DB. with category id" + categoryDetails.categoryId); return(categoryDetails); } //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(); //saves the details to DB dbContext.SaveChanges(); categoryDetails.categoryId = categoryEntity.categoryId; categoryDetails.categoryDesc = categoryEntity.categoryDesc; log.Info("The category is successfully created and saved in the DB. with category id" + categoryDetails.categoryId); return(categoryDetails); } catch (Exception e) { log.Error("The category creation failed:" + e.StackTrace); ErrorHandler customError = new ErrorHandler("DB error", e.Message); throw new WebFaultException <ErrorHandler>(customError, HttpStatusCode.BadRequest); } } }
/// <summary> /// Retrieves all the animals /// </summary> /// <returns>List of animals </returns> public List <Animal> RetrieveAllAnimals() { log.Info("Retrieve all the animsl : RetrieveAllAnimals"); List <Animal> animalList = new List <Animal>(); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { //fetches the animal from the DB string query = String.Format("SELECT tblanimal.categoryId,tblanimal.gpsDeviceId,tblanimal.animalName,tblanimal.animalId, tblanimal.gpsDeviceId, tblanimal.createdAt, tblcategory.categoryName FROM tblanimal INNER JOIN tblcategory on tblanimal.categoryId = tblcategory.categoryId order by createdAt"); animalList = dbContext.Database.SqlQuery <Animal>(query).ToList <Animal>(); log.Info("Successfully retrieved the animal details"); return(animalList); } catch (Exception ex) { log.Error("Error in retrieving the animals: " + ex.StackTrace); ErrorHandler error = new ErrorHandler("Error Info", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.NotFound); } } }
/// <summary> /// Retrieves animal count per category /// </summary> /// <param name="animalDetails">The animal details to be deleted</param> /// <returns>The details of updated animal</returns> public List <AnimalCount> RetrieveAnimalsCountPerCategory(string fromDate, string toDate) { log.Info("Retrieves the animals details per category : RetrieveAnimalsCountPerCategory"); List <AnimalCount> animalList = new List <AnimalCount>(); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { //Fetches the details of all animals between the starting and ending period. string sqlQuery = String.Format("SELECT tblcategory.categoryId, tblcategory.colorIndication,tblcategory.categoryName, COUNT(*) as totalAnimals FROM tblanimal INNER JOIN tblcategory ON tblcategory.categoryId = tblanimal.categoryId where DATE(tblanimal.createdAt) >= '{0}' and DATE(tblanimal.createdAt) <= '{1}' GROUP BY tblcategory.categoryId;", fromDate, toDate); animalList = dbContext.Database.SqlQuery <AnimalCount>(sqlQuery).ToList <AnimalCount>(); log.Info("Retrieved the details of all animals from : " + fromDate + " to : " + toDate); return(animalList); } catch (Exception ex) { log.Error("Error in retrieving the animals count" + ex.StackTrace); ErrorHandler customError = new ErrorHandler("DB error", ex.Message); throw new WebFaultException <ErrorHandler>(customError, HttpStatusCode.NotFound); } } }
/// <summary> /// Retrieves all the animal's latest position per category /// </summary> /// /// <param name="categoryId">The category id</param> /// <returns>List of Tracking info</returns> public List <GPSTrackingInfo> RetrieveAllAnimalsLatestLocationPerCategory(string categoryId) { log.Info("Retrieve all the animal's latest position : RetrieveAllAnimalsLatestLocationPerCategory"); List <GPSTrackingInfo> latestAnimalPosition = new List <GPSTrackingInfo>(); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { //fetches the GPS location string query = String.Format("SELECT category.categoryId ,category.categoryName,category.colorIndication,gpsTrack.trackingId,gpsTrack.createdAt,gpsTrack.animalId,gpsTrack.latitude,gpsTrack.longitude,animal.gpsDeviceId, animal.animalName FROM tblgpstracking AS gpsTrack , tblcategory as category, tblanimal as animal where category.categoryId= '{0}' and category.categoryId = animal.categoryId and animal.animalId = gpsTrack.animalId group by gpsTrack.animalId order by gpsTrack.createdAt desc", categoryId); latestAnimalPosition = dbContext.Database.SqlQuery <GPSTrackingInfo>(query).ToList <GPSTrackingInfo>(); log.Info("Successfully retrieved the GPS tracking Info"); return(latestAnimalPosition); } catch (Exception ex) { log.Error("Error in retrieving the tracking info: " + ex.StackTrace); ErrorHandler error = new ErrorHandler("Error", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.NotFound); } } }
/// <summary> /// Retrieves all the animal's latest position for all the categories /// </summary> /// <returns>List of Tracking info</returns> public List <GPSTrackingInfo> RetrieveAllAnimalsLatestLocation() { log.Info("Retrieve all the animal's latest position : RetrieveAllAnimalsLatestLocation"); List <GPSTrackingInfo> latestAnimalPosition = new List <GPSTrackingInfo>(); using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { string query = String.Format("SELECT category.categoryId ,category.categoryName,category.colorIndication,gpsTrack.trackingId,gpsTrack.createdAt,gpsTrack.animalId,gpsTrack.latitude,gpsTrack.longitude,animal.gpsDeviceId FROM tblgpstracking AS gpsTrack INNER JOIN tblanimal AS animal ON gpsTrack.animalId = animal.animalId INNER JOIN tblcategory as category on category.categoryId = animal.categoryId INNER JOIN ( SELECT trackingId, animalId, MAX( createdAt ) as MaxDate FROM tblgpstracking GROUP BY animalId) AS gpsLoc ON gpsTrack.animalId = gpsLoc.animalId AND gpsTrack.createdAt = gpsLoc.MaxDate"); //fetches the GPS location latestAnimalPosition = dbContext.Database.SqlQuery <GPSTrackingInfo>(query).ToList <GPSTrackingInfo>(); log.Info("Successfully retrieved the GPS tracking Info"); return(latestAnimalPosition); } catch (Exception ex) { log.Error("Error in retrieving the tracking info: " + ex.StackTrace); ErrorHandler error = new ErrorHandler("Error", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.NotFound); } } }
/// <summary> /// This method is used to retrieve the category detail for the specified categoryId. /// </summary> /// /// <param name="categoryId">The category id for which the details need to be fetched</param> /// <returns>The category details for the specified category Id</returns> public Category RetrieveCategory(string categoryId) { log.Info("Retrieve category details for " + categoryId + " : RetrieveAllCategories"); Int32 catId = Convert.ToInt32(categoryId); Category categoryDetails; using (game_reserve_dbEntities dbContext = new game_reserve_dbEntities()) { try { //Fetches the category details from DB var category = (from p in dbContext.tblcategories where p.categoryId == catId select p).SingleOrDefault(); categoryDetails = JsonConvert.DeserializeObject <Category>(JsonConvert.SerializeObject(category)); log.Info("Successfully retrieved the category details for category Id " + categoryId); return(categoryDetails); } catch (Exception ex) { log.Error("Error in retrieving the category for category Id " + categoryId); ErrorHandler error = new ErrorHandler("Error Info", ex.Message); throw new WebFaultException <ErrorHandler>(error, HttpStatusCode.BadRequest); } } }