Advertisement
Venciity

Untitled

Apr 10th, 2015
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.60 KB | None | 0 0
  1. namespace MusicSystem.Services.Controllers
  2. {
  3.     using MusicSystem.Data;
  4. using MusicSystem.Data.Repositories;
  5. using MusicSystem.Entities;
  6. using MusicSystem.Services.Models;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Net.Http;
  12. using System.Web.Http;
  13.  
  14.     public class SongsController : ApiController
  15.     {
  16.         private IMusicSystemData data;
  17.  
  18.         public SongsController()
  19.             :this(new MusicSystemData())
  20.         {
  21.         }
  22.  
  23.         public SongsController(IMusicSystemData data)
  24.         {
  25.             this.data = data;
  26.         }
  27.  
  28.         [HttpGet]
  29.         public IHttpActionResult All()
  30.         {
  31.             var songs = this.data.Songs.All().Select(SongModel.FromSong);
  32.             return Ok(songs);
  33.         }
  34.  
  35.         [HttpPost]
  36.         public IHttpActionResult Create(SongModel song)
  37.         {
  38.             if (!this.ModelState.IsValid)
  39.             {
  40.                 return BadRequest(ModelState);
  41.             }
  42.  
  43.             var newSong = new Song
  44.             {
  45.                 Title = song.Title,
  46.                 Year = song.Year,
  47.                 Genre = song.Genre,
  48.                 ArtistId = song.ArtistId
  49.             };
  50.  
  51.             this.data.Songs.Add(newSong);
  52.             this.data.Songs.SaveChanges();
  53.  
  54.             song.Id = newSong.Id;
  55.             return Ok(song);
  56.         }
  57.  
  58.         [HttpPut]
  59.         public IHttpActionResult Update(int id, SongModel song)
  60.         {
  61.             if (!this.ModelState.IsValid)
  62.             {
  63.                 return BadRequest(ModelState);
  64.             }
  65.  
  66.             var existingSong = this.data.Songs.All().FirstOrDefault(s => s.Id == id);
  67.             if (existingSong == null)
  68.             {
  69.                 return BadRequest("Such song does not exists!");
  70.             }
  71.  
  72.             existingSong.Title = song.Title;
  73.             existingSong.Year = song.Year;
  74.             existingSong.Genre = song.Genre;
  75.             existingSong.ArtistId = song.ArtistId;
  76.             this.data.Songs.SaveChanges();
  77.             song.Id = id;
  78.  
  79.             return Ok(song);
  80.         }
  81.  
  82.         [HttpDelete]
  83.         public IHttpActionResult Delete(int id)
  84.         {
  85.             // create find method in repository later
  86.             var existingSong = this.data.Songs.All().FirstOrDefault(s => s.Id == id);
  87.             if (existingSong == null)
  88.             {
  89.                 return BadRequest("Such song does not exists!");
  90.             }
  91.  
  92.             this.data.Songs.Delete(existingSong);
  93.             this.data.Songs.SaveChanges();
  94.  
  95.             return Ok();
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement