Пример #1
0
        // POST api/Album
        public HttpResponseMessage PostAlbum(Album album)
        {
            // header
            //User-Agent: Fiddler
            //Host: localhost:11302
            //Content-type: application/json
            //Content-Length: 97

            // req body
            //{
            //"Title": "I Love WebServices",
            //"ReleaseDate": "2002.11.03",
            //"Producer": "The Big Chicken"
            //}

            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();

                HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, album);
                response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = album.AlbumId }));
                return response;
            }
            else
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }
        }
Пример #2
0
        private static string EnterAlbum(RequestConsumer reqConsumer, string controller)
        {
            Console.WriteLine("Enter title: ");
            string title = Console.ReadLine();
            Console.WriteLine("Enter producer(optional): ");
            string producer = Console.ReadLine();
            Console.WriteLine("Enter release date(optional): ");
            DateTime? releaseDate = null;
            try
            {
                releaseDate = DateTime.Parse(Console.ReadLine());
            }
            catch (FormatException ex)
            {

            }

            Album newAlbum = new Album()
            {
                Title = title,
                Producer = producer,
                ReleaseDate = releaseDate,
            };

            Console.WriteLine("As Json(1) Or XML(2)? ");
            string choice = Console.ReadLine();
            if (choice == "1")
            {
                var sent = reqConsumer.CreateAsJson<Album>(newAlbum, controller);
                return sent;
            }
            else
            {
                var sent = reqConsumer.CreateAsXML<Album>(newAlbum, controller);
                return sent;
            }
        }
Пример #3
0
        // PUT api/Album/5
        public HttpResponseMessage PutAlbum(int id, Album album)
        {
            // header
            //User-Agent: Fiddler
            //Host: localhost:11302
            //Content-type: application/json
            //Content-Length: 177

            // req body
            //{
            //"AlbumId": "1",
            //"Title": "I Love WebServicessss",
            //"ReleaseDate": "2002.11.03",
            //"Producer": "The Big Chicken"
            //}

            if (!ModelState.IsValid)
            {
                return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState);
            }

            if (id != album.AlbumId)
            {
                return Request.CreateResponse(HttpStatusCode.BadRequest);
            }

            db.Entry(album).State = EntityState.Modified;

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException ex)
            {
                return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex);
            }

            return Request.CreateResponse(HttpStatusCode.OK);
        }
Пример #4
0
        private static string UpdateAlbum(RequestConsumer reqConsumer, string controller)
        {
            Console.Write("Enter id: ");
            var inputId = int.Parse(Console.ReadLine());
            Console.WriteLine("Enter title: ");
            string title = Console.ReadLine();
            Console.WriteLine("Enter producer(optional): ");
            string producer = Console.ReadLine();
            Console.WriteLine("Enter release date(optional): ");
            DateTime? releaseDate = null;
            try
            {
                releaseDate = DateTime.Parse(Console.ReadLine());
            }
            catch (FormatException ex)
            {

            }

            Console.WriteLine("Artist Name(optional): ");
            string artistName = Console.ReadLine();

            MusicStoreContext db = new MusicStoreContext();

            var artist = (from a in db.Artists
                          where a.Name == artistName
                          select a
                        ).ToList();

            Album newAlbum;

            if (artist == null)
            {
                newAlbum = new Album()
                {
                    AlbumId = inputId,
                    Title = title,
                    Producer = producer,
                    ReleaseDate = releaseDate
                };
            }
            else
            {
                List<Artist> artists = new List<Artist>();
                foreach (var art in artist)
                {
                    artists.Add(CreateArtistObject(artist[0].ArtistId, artist[0].Name, artist[0].Country, artist[0].DateOfBirth));
                }

                newAlbum = new Album()
                {
                    AlbumId = inputId,
                    Title = title,
                    Producer = producer,
                    ReleaseDate = releaseDate,
                    Artists = artists
                };
            }

            Console.WriteLine("As Json(1) Or XML(2)? ");
            string choice = Console.ReadLine();
            if (choice == "1")
            {
                var sent = reqConsumer.UpdateAsJson<Album>(newAlbum, controller, inputId.ToString());
                return sent;
            }
            else
            {
                var sent = reqConsumer.UpdateAsXML<Album>(newAlbum, controller, inputId.ToString());
                return sent;
            }
        }