// GET api/topic/2 public HttpResponseMessage Get(int id) { using (var db = new TopicsDataContext()) { var topic = db.Topics.SingleOrDefault(t => t.Id == id); if (topic == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, new { message = "The topic you requested was not found" })); } return(Request.CreateResponse(HttpStatusCode.OK, new { id = topic.Id, name = topic.Name, tutorials = topic.Tutorials .Select(t => new { id = t.Id, name = t.Name, website = t.WebSite, type = t.Type, url = t.Url }) })); } }
// GET api/topic/2 public HttpResponseMessage Get(int id, string name) { using (var db = new TopicsDataContext()) { var tutorials = db.Topics.Where(t => t.Id == id) .SelectMany(t => t.Tutorials) .Where(t => t.Name == name) .ToList(); if (tutorials.Count == 0) { return(Request.CreateResponse(HttpStatusCode.NotFound, new { message = "The tutorial you requested was not found" })); } return(Request.CreateResponse(HttpStatusCode.OK, new { id = id, name = name, tutorials = tutorials .Select(t => new { id = t.Id, name = t.Name, website = t.WebSite, type = t.Type, url = t.Url }) })); } }
private static void MoveDatabaseToIsolatedStorage(string dbName) { var dBConnectionString = "Data Source=isostore:/" + dbName; using (var db = new TopicsDataContext(dBConnectionString)) { if (db.DatabaseExists()) { return; } } // Obtain the virtual store for the application. IsolatedStorageFile iso = IsolatedStorageFile.GetUserStoreForApplication(); // Create a stream for the file in the installation folder. using (Stream input = Application.GetResourceStream(new Uri(dbName, UriKind.Relative)).Stream) { // Create a stream for the new file in isolated storage. using (IsolatedStorageFileStream output = iso.CreateFile(dbName)) { // Initialize the buffer. byte[] readBuffer = new byte[input.Length]; int bytesRead = -1; // Copy the file from the installation folder to isolated storage. while ((bytesRead = input.Read(readBuffer, 0, readBuffer.Length)) > 0) { output.Write(readBuffer, 0, bytesRead); } } } //MoveDirectoryToIsolatedStorage("Databases//DrivingTheory//Images"); }
// POST api/values public HttpResponseMessage Post(Topic topic) { using (var db = new TopicsDataContext()) { db.Topics.Add(topic); db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, new { id = topic.Id, url = Request.RequestUri.AbsoluteUri + "/" + topic.Id })); } }
public HttpResponseMessage Put(Topic topic) { using (var db = new TopicsDataContext()) { db.Topics.Attach(topic); db.Entry(topic).State = EntityState.Modified; db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, new { message = "topic is updated successfully" })); } }
public HttpResponseMessage Get() { using (var db = new TopicsDataContext()) { var topics = db.Topics .Select(t => new { id = t.Id, name = t.Name }) .ToList(); return(Request.CreateResponse(HttpStatusCode.OK, topics)); } }
// DELETE api/topic/2 public HttpResponseMessage Delete(int id) { using (var db = new TopicsDataContext()) { var topic = db.Topics.SingleOrDefault(t => t.Id == id); if (topic == null) { return(Request.CreateResponse(HttpStatusCode.NotFound, new { message = "The topic you want to delete was not found" })); } db.Topics.Attach(topic); db.Entry(topic).State = EntityState.Deleted; db.SaveChanges(); return(Request.CreateResponse(HttpStatusCode.OK, new { message = "The topic was deleted successfully" })); } }
public void LoadFrom(string dataBaseName) { string dBConnectionString = "Data Source=isostore:/" + dataBaseName; //Name = dataBaseName; topicsDb = new TopicsDataContext(dBConnectionString); Name = topicsDb.Databases.ToList()[0].Name; LoadCollectionsFromDatabase(); CreateStatasticsTable(); CalculateTotalQuestions(); CalculatePercentageWorked(); CalculateSuccessPercentage(); }