예제 #1
0
        public ActionResult <SpotifyTrendingSong> Post([FromBody] SpotifyTrendingSong newSong)
        {
            this.HttpContext.Request.Headers.TryGetValue("Accept", out var accept);

            switch (accept)
            {
            case "application/json":
                Response.Headers.Add("link", JSON_SCHEMA);
                break;

            case "application/xml":
                Response.Headers.Add("link", XML_SCHEMA);
                break;

            default:
                return(BadRequest("Invalid accept header! (application/xml OR application/json)"));
            }

            // check ID
            // error on exist
            // return new object on success.
            if (database.SpotifyData.Any(x =>
                                         x.Position == newSong.Position &&
                                         x.Date == newSong.Date &&
                                         x.Region == newSong.Region))
            {
                return(Conflict("Song with this Region/date/postion combo already exists!"));
            }

            database.SpotifyData.Add(newSong);
            database.SaveChanges();

            return(newSong);
        }
예제 #2
0
        public ActionResult <SpotifyTrendingSong> Put([FromBody] SpotifyTrendingSong updatedSong)
        {
            // Add schema header related to accept data type
            this.HttpContext.Request.Headers.TryGetValue("Accept", out var accept);

            switch (accept)
            {
            case "application/json":
                Response.Headers.Add("link", JSON_SCHEMA);
                break;

            case "application/xml":
                Response.Headers.Add("link", XML_SCHEMA);
                break;

            default:
                return(BadRequest("Invalid accept header! (application/xml OR application/json)"));
            }

            // if no such value to update, return an error.
            if (!database.SpotifyData.Any(x =>
                                          x.Position == updatedSong.Position &&
                                          x.Date == updatedSong.Date &&
                                          x.Region == updatedSong.Region))
            {
                return(Conflict("No such song with this Region/date/postion combo to update!"));
            }

            // Get the old value
            var oldSong = database.SpotifyData.First(x =>
                                                     x.Position == updatedSong.Position &&
                                                     x.Date == updatedSong.Date &&
                                                     x.Region == updatedSong.Region);

            // Update with the new value
            database.SpotifyData.Update(updatedSong);
            database.SaveChanges();

            // Return the old value
            return(oldSong);
        }