/// <summary> /// Handles DELETE http method /// </summary> /// <param name="context">Context of incoming http request</param> private void DeleteHandler(HttpContext context) { try { ReviewList.Delete(Int32.Parse(context.Request.RawUrl.Split('/'). Last())); context.Response.Write("200: Review 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> /// 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( ReviewList.ReadAll(), Formatting.Indented, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore })); } else { context.Response.Write(JsonConvert.SerializeObject( ReviewList.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> /// Deletes an specific song and all its reviews /// </summary> /// <param name="songId">Id of specific song to delete</param> public static void Delete(int songId) { Song song = songs.Find(s => s.SongId == songId); if (song != null) { foreach (Review review in song.Reviews) { ReviewList.ReadAll().Remove(review); } songs.Remove(song); } else { throw new ArgumentException("Resource not found."); } }
/// <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 text = HttpUtility.ParseQueryString(queryString). Get("text"); Review review = new Review(Int32.Parse(context.Request.RawUrl. Split('/').Last()), text); ReviewList.Update(Int32.Parse(context.Request.RawUrl.Split('/'). Last()), review); context.Response.Write("200: Review 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."); } }
/// <summary> /// Handles POST http method /// </summary> /// <param name="context">Context of incoming http request</param> private void PostHandler(HttpContext context) { try { string queryString = new StreamReader(context.Request. InputStream).ReadToEnd(); string songId = HttpUtility.ParseQueryString(queryString). Get("songId"); string text = HttpUtility.ParseQueryString(queryString). Get("text"); Review review = new Review(ReviewList.GenerateId(), text); ReviewList.Create(review, Int32.Parse(songId)); context.Response.Write("200: Review Created."); } 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."); } }