I need to write ASP Web Api code which will take users input in this format:
"GenreName" : "Rock",
"Songs":[
"SongName":"Song 1",
"Length":233,
"Composer":"Composer 1"
],
[
"SongName":"Song 2",
"Length":221,
"Composer":"Composer 2"
]
The values are being sent with Postman using JSON. I have figured out to use string[] SongName to add list of songs, but i need to pass additional keys so all columns in SQL can be filled such as length and composer. The thing troubling me is how to place all these values into array and then loop them with foreach. Below is the code i have managed to come up so far:
public void Execute(InsertDTO request)
{
var genre = request.GenreName;
var datum = request.Datum;
var trackname = request.TrackName;
//var add = new Genre
//{
// Name = genre
//};
//context.Genre.Add(add);
// int id = add.GenreId;
var check = context.Genre.Where(x=>x.Name == request.GenreName).Count();
// var chek2= context.Track.Where(x=>x.Name==request.Tracks.con)
if (datum > DateTime.Now)
{
throw new ArgumentException("datum ne moze biti u buducnosti");
}
else
{
Genre g = new Genre
{
Name = genre,
Datum = datum
};
context.Genre.Add(g);
context.SaveChanges();
var id = g.GenreId;
foreach (var t in trackname) {
var pesma = t;
Track track = new Track
{
MediaTypeId=1,
GenreId = id,
Name=pesma
};
context.Track.Add(track);
context.SaveChanges();
}
}
}
InsertDTO.cs
public class InsertDTO
{
// public int GenreId { get; set; }
public string GenreName { get; set; }
public DateTime Datum { get; set; }
public string[] TrackName { get; set; }
}
}
Help is greatly appreciated!