I have made a method that can be included in the service to add artists the fan has selected to the fanArtist table.The code is commented to indicate what is going on.
public int AddFanArtist( int fanKey, string artistName)
{
/*********************************
* This method will add an artist to the artistFan
* table. First we have to find the fan
* and then the particular artist
* Then we add the artist to the Fan's list
* of artists to follow
* **********************************/
int result = 1;
//get the fan. the key can come from their login
Fan myFan = (from f in se.Fans
where f.FanKey == fanKey
select f).First();
//get the artist by name
Artist myArtist = (from a in se.Artists
where a.ArtistName.Equals(artistName)
select a).First();
//add the artist to the fan;'s collection of artists
myFan.Artists.Add(myArtist);
//save the changes
se.SaveChanges();
return result;
}
}
I also made a client method to show how you could use this. I used a CheckBoxList to select the artists from. Here is a picture of that on the web form
Obviously this could be made to look better. Here is the asp source code for the page
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>Select your artists and click enter to add them</p>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" RepeatColumns="3"></asp:CheckBoxList>
<asp:Button ID="Button1" runat="server" Text="Add Artists" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
Here is the code behind
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
ServiceReference1.FanArtistServiceClient sc = new ServiceReference1.FanArtistServiceClient();
protected void Page_Load(object sender, EventArgs e)
{
//I hard coded the key in so I didn't have to do the login
//for this example
Session["key"] = 2;
if (!IsPostBack)
PopulateArtists();
}
protected void Button1_Click(object sender, EventArgs e)
{
AddArtists();
}
protected void PopulateArtists()
{
//this method populates the CheckboxList
//with artist names
string[] artists = sc.GetArtist();
CheckBoxList1.DataSource = artists;
CheckBoxList1.DataBind();
}
protected void AddArtists()
{
//get the fan's key
int key = (int)Session["key"];
//loop through the checkboxList
//to see what's checked
foreach(ListItem i in CheckBoxList1.Items)
{
//if it is checked call the service method to add
//it to the database
if(i.Selected)
{
int x = sc.AddFanArtist(key, i.Text);
}
}
Label1.Text = "Artist have been added";
CheckBoxList1.Items.Clear();
}
}
The next thing to do will be to make a query of all the artists and their shows for a particular fan. Here is my first take on that method. It works but could be made more elegant perhaps. This method also goes in the service.
public List<ShowInfo> GetShowsForFanArtists(int fanKey)
{
//get the fan
Fan myFan = (from f in se.Fans
where f.FanKey == fanKey
select f).First();
List<ShowInfo> shows = new List<ShowInfo>();
//this loop within a loop is very inefficient
foreach(Artist a in myFan.Artists)
{
//get all the shows for the fan
var shws = from s in se.Shows
from sd in s.ShowDetails
where sd.ArtistKey == a.ArtistKey
select new
{
s.ShowName,
s.ShowTime,
s.ShowDate,
s.ShowTicketInfo,
s.Venue.VenueName,
sd.Artist.ArtistName
};
//loop through the shows and write them to
//ShowInfo objects then add those objects
//to the list
foreach(var sh in shws)
{
ShowInfo info = new ShowInfo();
info.ShowName = sh.ShowName;
info.ShowDate = sh.ShowDate.ToString();
info.ShowTime = sh.ShowTime.ToString();
info.TicketInfo = sh.ShowTicketInfo;
info.VenueName = sh.VenueName;
info.ArtistName = sh.ArtistName;
shows.Add(info);
}
}
return shows;
}