public ActorsModule(IMongoDatabase database) : base("/api") { // GET api/actors Get["/actors", true] = async (parameters, ct) => { var actorsDb = new ActorDAO(database); var actorList = await actorsDb.GetActorsList(); return JsonConvert.SerializeObject(actorList); }; // GET api/actors/name/actorname Get["/actors/name/{name*}", true] = async (parameters, ct) => { var actorsDb = new ActorDAO(database); var actorList = await actorsDb.GetActorsList(); var filteredList = actorList.Where(t => t.name.Contains(parameters.name)); return JsonConvert.SerializeObject(filteredList); }; // GET api/actors/5 Get["/actors/{id}", true] = async (parameters, ct) => { var actorsDb = new ActorDAO(database); var actorList = await actorsDb.GetActorsList(); var actor = actorList.FirstOrDefault(t => t.actorId == parameters.id); var moviesDb = new MovieDAO(database); var movieList = await moviesDb.GetMoviesList(); var keyMovies = movieList .Where(m => m.keyActors != null) .SelectMany( m => m.keyActors.Where( a => a.actorId == actor.actorId), (m, a) => m); if (keyMovies != null) actor.keyMovies = keyMovies.ToList(); return JsonConvert.SerializeObject(actor); }; // POST api/actors Post["/actors", true] = async (parameters, ct) => { try { Actor actor = this.Bind<Actor>(); // Nancy.ModelBinding var actorsDb = new ActorDAO(database); var actorInserted = await actorsDb.PostActor(actor); return Negotiate.WithModel(actorInserted) .WithStatusCode(HttpStatusCode.Created); } catch { return HttpStatusCode.InternalServerError; } }; // PUT api/actors/5 Put["/actors/{id}", true] = async (parameters, ct) => { try { Actor actor = this.Bind<Actor>(); actor.actorId = parameters.id; var actorsDb = new ActorDAO(database); string updateResult = await actorsDb.PutActor(actor); return Negotiate.WithModel(updateResult) .WithStatusCode(HttpStatusCode.OK); } catch { return HttpStatusCode.InternalServerError; } }; // DELETE api/actors/5 Delete["/actors/{id}", true] = async (parameters, ct) => { try { var actorsDb = new ActorDAO(database); string deleteResult = await actorsDb.DeleteActor(parameters.id); return Negotiate.WithModel(deleteResult) .WithStatusCode(HttpStatusCode.OK); } catch { return HttpStatusCode.InternalServerError; } }; }
public MoviesModule(IMongoDatabase database) : base("/api") { // GET api/movies Get["/movies", true] = async (parameters, ct) => { var moviesDb = new MovieDAO(database); var movieList = await moviesDb.GetMoviesList(); return JsonConvert.SerializeObject(movieList); }; // GET api/movies/title/moviename Get["/movies/title/{title*}", true] = async (parameters, ct) => { var moviesDb = new MovieDAO(database); var movieList = await moviesDb.GetMoviesList(); var filteredList = movieList.Where(t => t.title.Contains(parameters.title)); return JsonConvert.SerializeObject(filteredList); }; // GET api/movies/5 Get["/movies/{id}", true] = async (parameters, ct) => { var moviesDb = new MovieDAO(database); var movieList = await moviesDb.GetMoviesList(); var movie = movieList.FirstOrDefault(t => t.movieId == parameters.id); var actorsDb = new ActorDAO(database); var actorList = await actorsDb.GetActorsList(); if(movie.keyActors != null) { var populatedActorList = new List<Models.Actor>(); foreach (var item in movie.keyActors) { var actor = actorList.FirstOrDefault(a => a.actorId == item.actorId); populatedActorList.Add(actor); } movie.keyActors = populatedActorList; } return JsonConvert.SerializeObject(movie); }; // POST api/movies Post["/movies", true] = async (parameters, ct) => { try { Movie movie = this.Bind<Movie>(); // Nancy.ModelBinding var moviesDb = new MovieDAO(database); var movieInserted = await moviesDb.PostMovie(movie); return Negotiate.WithModel(movieInserted) .WithStatusCode(HttpStatusCode.Created); } catch { return HttpStatusCode.InternalServerError; } }; // PUT api/movies/5 Put["/movies/{id}", true] = async (parameters, ct) => { try { Movie movie = this.Bind<Movie>(); movie.movieId = parameters.id; var moviesDb = new MovieDAO(database); string updateResult = await moviesDb.PutMovie(movie); return Negotiate.WithModel(updateResult) .WithStatusCode(HttpStatusCode.OK); } catch { return HttpStatusCode.InternalServerError; } }; // DELETE api/movies/5 Delete["/movies/{id}", true] = async (parameters, ct) => { try { var moviesDb = new MovieDAO(database); string deleteResult = await moviesDb.DeleteMovie(parameters.id); return Negotiate.WithModel(deleteResult) .WithStatusCode(HttpStatusCode.OK); } catch { return HttpStatusCode.InternalServerError; } }; }