Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using AutoMapper;
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web.Http;
- using Vidly.Dtos;
- using Vidly.Models;
- namespace Vidly.Controllers.Api
- {
- public class MoviesController : ApiController
- {
- private ApplicationDbContext _context;
- public MoviesController()
- {
- _context = new ApplicationDbContext();
- }
- public IEnumerable<MoviesDto> GetMovies()
- {
- return _context.Movies.ToList().Select(Mapper.Map<Movie, MoviesDto>);
- }
- public IHttpActionResult GetMovie(int id)
- {
- var movie = _context.Movies.SingleOrDefault(c => c.Id == id);
- if (movie == null)
- return NotFound();
- return Ok(Mapper.Map<Movie, MoviesDto>(movie));
- }
- [HttpPost]
- public IHttpActionResult CreateMovie(MoviesDto movieDto)
- {
- if (!ModelState.IsValid)
- return BadRequest();
- var movie = Mapper.Map<MoviesDto, Movie>(movieDto);
- _context.Movies.Add(movie);
- _context.SaveChanges();
- movieDto.Id = movie.Id;
- return Created(new Uri(Request.RequestUri + "/" + movie.Id), movieDto);
- }
- [HttpPut]
- public IHttpActionResult UpdateMovie(int id, MoviesDto movieDto)
- {
- if (!ModelState.IsValid)
- return BadRequest();
- var movieInDb = _context.Movies.SingleOrDefault(c => c.Id == id);
- if (movieInDb == null)
- return NotFound();
- Mapper.Map(movieDto, movieInDb);
- _context.SaveChanges();
- return Ok();
- }
- [HttpDelete]
- public IHttpActionResult DeleteMovie(int id)
- {
- var movieInDb = _context.Movies.SingleOrDefault(c => c.Id == id);
- if (movieInDb == null)
- return NotFound();
- _context.Movies.Remove(movieInDb);
- _context.SaveChanges();
- return Ok();
- }
- }
- }
Add Comment
Please, Sign In to add comment