예제 #1
0
        /// <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.");
            }
        }
예제 #2
0
 /// <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.");
     }
 }