/// <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> /// 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> /// 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> /// 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 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); } } }