/// <summary> /// Handles DELETE http method /// </summary> /// <param name="context">Context of incoming http request</param> private void DeleteHandler(HttpContext context) { try { SongList.Delete(Int32.Parse(context.Request.RawUrl.Split('/'). Last())); context.Response.Write("Song Deleted."); } catch (ArgumentNullException) { context.Response.StatusCode = 400; context.Response.Write("Error: Input values are missing."); } catch (ArgumentException err) { context.Response.StatusCode = 404; context.Response.Write("Error: " + err.Message); } catch (FormatException err) { context.Response.StatusCode = 500; context.Response.Write("Error: " + err.Message); } catch (Exception) { context.Response.StatusCode = 500; context.Response.Write("Error: Server had a problem. Please try " + "again later."); } }
/// <summary> /// Creates a review for an specific song /// </summary> /// <param name="review">Review to create</param> /// <param name="songId">Id of the specific song</param> public static void Create(Review review, int songId) { Song song = SongList.ReadAll().Find(s => s.SongId == songId); if (song != null) { review.Song = song; song.Reviews.Add(review); reviews.Add(review); } else { throw new ArgumentException("Resource not found."); } }
/// <summary> /// Handles GET http method /// </summary> /// <param name="context">Context of incoming http request</param> private void GetHandler(HttpContext context) { try { if (context.Request.RawUrl.Split('/').Last() == RESOURCE_URL) { context.Response.Write(JsonConvert.SerializeObject(SongList. ReadAll(), Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); } else { context.Response.Write(JsonConvert.SerializeObject(SongList. Read(Int32.Parse(context.Request.RawUrl.Split('/'). Last())), Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); } } catch (ArgumentNullException) { context.Response.StatusCode = 400; context.Response.Write("Error: Input values are missing."); } catch (ArgumentException err) { context.Response.StatusCode = 404; context.Response.Write("Error: " + err.Message); } catch (FormatException err) { context.Response.StatusCode = 500; context.Response.Write("Error: " + err.Message); } catch (Exception) { context.Response.StatusCode = 500; context.Response.Write("Error: Server had a problem. Please try " + "again later."); } }
/// <summary> /// Handles PUT http method /// </summary> /// <param name="context">Context of incoming http request</param> private void PutHandler(HttpContext context) { try { string queryString = new StreamReader(context.Request. InputStream).ReadToEnd(); string name = HttpUtility.ParseQueryString(queryString). Get("name"); string artist = HttpUtility.ParseQueryString(queryString). Get("artist"); string duration = HttpUtility.ParseQueryString(queryString). Get("duration"); string genre = HttpUtility.ParseQueryString(queryString). Get("genre"); string path = HttpUtility.ParseQueryString(queryString). Get("path"); Song song = new Song(Int32.Parse(context.Request.RawUrl. Split('/').Last()), name, artist, Int32.Parse(duration), genre, BASE_PATH + path); SongList.Update(Int32.Parse(context.Request.RawUrl.Split('/'). Last()), song); context.Response.Write("200: Song Updated."); } catch (ArgumentNullException) { context.Response.StatusCode = 400; context.Response.Write("Error: Input values are missing."); } catch (ArgumentException err) { context.Response.StatusCode = 404; context.Response.Write("Error: " + err.Message); } catch (FormatException err) { context.Response.StatusCode = 500; context.Response.Write("Error: " + err.Message); } catch (Exception) { context.Response.StatusCode = 500; context.Response.Write("Error: Server had a problem. Please try " + "again later."); } }