public ActionResult Create(Artist artist) { repository.add(artist); repository.saveChanges(); return RedirectToAction("Index"); }
public IHttpActionResult Post([FromBody]ArtistDataModel model) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } if (this.data.Countries.Find(model.CountryId) == null) { return this.BadRequest(GlobalContants.CountryNotFoundMessage); } var artist = new Artist { Name = model.Name, DateOfBirth = model.DateOfBirth, CountryId = model.CountryId, Albums = model.Albums }; this.data.Artists.Add(artist); this.data.Artists.SaveChanges(); return this.Created(this.Url.ToString(), artist); }
public static void AddArtistXml(HttpClient client) { client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml")); Console.WriteLine("Enter artist name"); string name = Console.ReadLine(); Console.WriteLine("Enter artist country"); string country = Console.ReadLine(); Console.WriteLine("Enter artist birth date"); DateTime birthDate = DateTime.ParseExact(Console.ReadLine(), "dd.MM.yyyy", new CultureInfo("en-US")); Artist artist = new Artist { Name = name, Country = country, DateOfBirth = birthDate }; HttpResponseMessage response = client.PostAsXmlAsync("api/artists", artist).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Artist added"); } else { Console.WriteLine("{0} ({1})", (int)response.StatusCode, response.ReasonPhrase); } }
public ActionResult Create(Artist artist) { if(!ModelState.IsValid) return View(artist); repository.Add(artist); repository.SaveChanges(); return RedirectToAction("Index"); }
private static void AddTwoSongs(HttpClient client, Random rand, Artist firstArtist) { var song1 = new Song { Title = "Song-" + GetRandomSuffix(rand), Artist = firstArtist, Year = rand.Next(1980,2013) }; var song2 = new Song { Title = "Song-" + GetRandomSuffix(rand), Artist = firstArtist, Year = rand.Next(1980, 2013) }; Console.WriteLine("\nTwo new songs"); SongsController.Add(song1, client); SongsController.Add(song2, client); Console.WriteLine(); }
public ArtistApiModel(Artist artist) { if (artist != null) { this.Id = artist.Id; this.Name = artist.Name; this.Country = artist.Country; this.DateOfBirth = artist.DateOfBirth; } }
public ActionResult Create(Artist artist) { if (ModelState.IsValid) { storeDB.AddToArtists(artist); storeDB.SaveChanges(); return RedirectToAction("Index"); } return View(); }
// This is without ninject //public ArtistsService() //{ // var db = new MusicStoreDbContext(); // this.artists = new GenericRepository<Artist>(db); //} public int Add(string name, string country) { var newArtist = new Artist { Name = name, Country = country }; this.artists.Add(newArtist); this.artists.SaveChanges(); return newArtist.Id; }
public static void Delete(Artist artist, HttpClient client) { var postArtist = client.DeleteAsync("Artists/" + artist.Id).Result; if (postArtist.IsSuccessStatusCode) { Console.WriteLine(postArtist.Headers.Location); } else { Console.WriteLine(postArtist.ReasonPhrase); } }
public ActionResult Edit(Artist artist) { if (!ModelState.IsValid) return View(artist); try { using (TransactionScope scope = new TransactionScope()) { repository.Update(artist); repository.SaveChanges(); scope.Complete(); } return RedirectToAction("Index"); } catch (System.Data.Entity.Infrastructure.DbUpdateConcurrencyException) { ViewBag.Message = "Sorry, that didn't work!<br />Adam beat you to the punch"; return View(artist); } }
public ArtistApi(Artist artist) : base(artist) { if (artist != null) { if (artist.Albums != null) { this.Albums = artist.Albums.Select(x => x.Title).ToList(); } if (artist.Songs != null) { this.Songs = artist.Songs.Select(x => x.Title).ToList(); } } }
public ArtistFullApi(Artist artist) : base(artist) { if (artist != null) { if (artist.Albums != null) { this.Albums = artist.Albums.Select(x => new AlbumApi(x)).ToList(); } if (artist.Songs != null) { this.Songs = artist.Songs.Select(x => new SongApi(x)).ToList(); } } }
public static void Update(Artist artist, HttpClient client) { var serializedContent = JsonConvert.SerializeObject(artist); var content = new StringContent(serializedContent, Encoding.UTF8, "application/json"); var putArtist = client.PutAsync("Artists/" + artist.Id, content).Result; if (putArtist.IsSuccessStatusCode) { Console.WriteLine(putArtist.Headers.Location); } else { Console.WriteLine(putArtist.ReasonPhrase); } }
public IHttpActionResult Post([FromBody]ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return this.BadRequest(this.ModelState); } var newArtist = new Artist { Name = artist.Name, Country = artist.Country, DateOfBirth = artist.DateOfBirth, Albums = artist.Albums }; this.repository.Insert(newArtist); this.repository.SaveChanges(); return this.Ok(newArtist); }
public IHttpActionResult Create(ArtistModel artist) { if (!ModelState.IsValid) { return BadRequest(ModelState); } var newArtist = new Artist { Name = artist.Name, Country = artist.Country, DateOfBirth = artist.DateOfBirth }; this.Data.Artists.Add(newArtist); this.Data.SaveChanges(); artist.Id = newArtist.Id; return Ok(artist); }
public static Artist GetOrCreateArtist(Artist artist, MusicStoreDb musicStoreDbContext) { Artist existingArtist = null; if (artist.Id != 0) { existingArtist = musicStoreDbContext.Artists.FirstOrDefault(a => a.Id == artist.Id); } else if (artist.Name != null) { existingArtist = musicStoreDbContext.Artists.FirstOrDefault(a => a.Name == artist.Name); } if (existingArtist != null) { return existingArtist; } musicStoreDbContext.Artists.Add(artist); musicStoreDbContext.SaveChanges(); return artist; }
public static void UpdateArtist(HttpClient client, int artistId, string name, string country, DateTime dateOfBirth) { var artist = new Artist() { Name = name, Country = country, DateOfBirth = dateOfBirth }; var content = new StringContent(JsonConvert.SerializeObject(artist)); content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue(ContentType); var response = client.PutAsync("api/artists/update/" + artistId, content).Result; if (response.IsSuccessStatusCode) { Console.WriteLine("Artist updated successfully."); } else { Console.WriteLine("Artist have not been updated."); } }
/// <summary> /// Create a new Artist object. /// </summary> /// <param name="artistId">Initial value of the ArtistId property.</param> /// <param name="name">Initial value of the Name property.</param> public static Artist CreateArtist(global::System.Int32 artistId, global::System.String name) { Artist artist = new Artist(); artist.ArtistId = artistId; artist.Name = name; return artist; }
// POST api/Aritsts public HttpResponseMessage PostArtist(Artist artist) { if (ModelState.IsValid) { db.Artists.Add(artist); try { db.SaveChanges(); } catch (DbUpdateException ex) { return Request.CreateErrorResponse(HttpStatusCode.Conflict, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, artist); response.Headers.Location = new Uri(Url.Link("DefaultApi", new { id = artist.Id })); return response; } else { return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ModelState); } }
public IHttpActionResult Create(ArtistRequestModel artist) { if (!this.ModelState.IsValid) { return BadRequest(ModelState); } var existingCountry = this.data .Countries .All() .FirstOrDefault(c => c.Id == artist.CountryId || c.Name == artist.Country.Name); if(existingCountry == null) { return BadRequest("Such country doen not exist in database!"); } var newArtist = new Artist { BirthDate = artist.BirthDate, Country = existingCountry, Name = artist.Name }; this.data.Artists.Add(newArtist); this.data.SaveChanges(); artist.Id = newArtist.Id; return Ok(artist); }
private static void UpdateRandomArtist(HttpClient client, Artist firstArtist) { firstArtist.Country = "Country" + (new Random()).Next(50); firstArtist.DateOfBirth = new DateTime((new Random()).Next(1950,1999), 1, 30); ArtistsController.Update(firstArtist, client); }
/// <summary> /// Deprecated Method for adding a new object to the Artists EntitySet. Consider using the .Add method of the associated ObjectSet<T> property instead. /// </summary> public void AddToArtists(Artist artist) { base.AddObject("Artists", artist); }
public List<Artist> GenerateArtists(IRandomGenerator generator, int count) { var artists = new List<Artist>(); for (var i = 0; i < count; i++) { var artist = new Artist() { Name = generator.GetRandomString(generator.GetRandomNumber(5, 20)), DateOfBirth = generator.GetRandomDateTime(DateTime.Now.AddYears(-90), DateTime.Now.AddYears(-20)).ToString(), Country = generator.GetRandomString(generator.GetRandomNumber(5, 20)), Albums = new List<Album>() { new Album() { Title = generator.GetRandomString(generator.GetRandomNumber(5, 20)), Year = generator.GetRandomNumber(DateTime.Now.AddYears(-50).Year, DateTime.Now.Year), Producer = generator.GetRandomString(generator.GetRandomNumber(5, 20)), Artists = new List<Artist>(), Songs = new List<Song>() } }, Songs = new List<Song>() { new Song() { Title = generator.GetRandomString(generator.GetRandomNumber(5, 20)), Genre = generator.GetRandomString(generator.GetRandomNumber(5, 20)), Year = generator.GetRandomNumber(DateTime.Now.AddYears(-50).Year, DateTime.Now.Year), ArtistId = null, AlbumId = null } } }; artists.Add(artist); } return artists; }