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