Skip to content
Permalink
Browse files

invoice track data API

  • Loading branch information
Jeremy Mill
Jeremy Mill committed Dec 10, 2015
1 parent 56a17a0 commit 57b6cbed1a6f30a0a02af4f46a9af03e1482ef5d
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using TeamDBAwesome.SqlService;
using TeamDBAwesome.Models;
using Newtonsoft.Json;

namespace TeamDBAwesome.Controllers
{
public class InvoiceTrackDataController : ApiController
{
public HttpResponseMessage Get(int invoiceId)
{
MySqlService dbService = new MySqlService();

List<Track> trackList = dbService.getInvoiceTracks(invoiceId);

string serialzed = JsonConvert.SerializeObject(trackList);

HttpResponseMessage message = new HttpResponseMessage(HttpStatusCode.OK);
message.Content = new StringContent(serialzed);

return message;

}
}
}
@@ -18,5 +18,6 @@ public class Track
public float UnitPrice { get; set; }
public string artist { get; set; }
public string url { get; set; }
public int qty { get; set; }
}
}
@@ -454,6 +454,41 @@ public int createInvoice(int custId, int payId, int orderId)
cmd = new MySqlCommand(insert, SqlConn);
cmd.ExecuteNonQuery();

string nextInvoiceLineId = "select InvoiceLineId from invoiceline order by invoicelineid desc limit 1";
cmd = new MySqlCommand(nextInvoiceLineId, SqlConn);
int nextLineId = int.Parse(cmd.ExecuteScalar() + "") + 1;

string invoiceLineQuery = "select track.trackid,track.UnitPrice,count(*) as count from ordertracks left join orders on ordertracks.OrderId = orders.orderid " +
"left join track on ordertracks.trackid = track.trackid " +
"where ordertracks.orderid = " + orderId + " " +
"group by trackid";
cmd = new MySqlCommand(invoiceLineQuery, SqlConn);
reader = cmd.ExecuteReader();
while(reader.Read())
{
int nextTrackId = int.Parse(GetDBString("trackid", reader));
int nextQty = int.Parse(GetDBString("count", reader));
float unitPrice = float.Parse(GetDBString("UnitPrice", reader));
float lineTotal = nextQty * unitPrice;
string invLineIns = "insert into invoiceline (InvoiceLineId,InvoiceId,TrackId,UnitPrice,Quantity) VALUES ("
+ nextLineId + "," + nextInvoice + "," + nextTrackId + "," + unitPrice + "," + nextQty + ")";

server = "localhost";
database = "chinook";
uid = "root";
//changeme
password = "";
string connectionString2 = "SERVER=" + server + ";" + "DATABASE=" + database + ";" + "UID=" + uid + ";" + "PASSWORD=" + password + ";";

MySqlConnection SqlConn2 = new MySqlConnection(connectionString2);
SqlConn2.Open();
MySqlCommand cmd_ins = new MySqlCommand(invLineIns, SqlConn2);
cmd_ins.ExecuteNonQuery();
SqlConn2.Close();
nextLineId = nextLineId + 1;
}
reader.Close();

//NEED TO DELETE THE ORDER, AND ORDER TRACKS
string delete_ordertracks = "delete from ordertracks where orderid = " + orderId;
cmd = new MySqlCommand(delete_ordertracks, SqlConn);
@@ -470,6 +505,34 @@ public int createInvoice(int custId, int payId, int orderId)
return 0;
}
}

public List<Track> getInvoiceTracks(int invoiceId)
{
List<Track> trackList = new List<Track>();
bool open = this.OpenConnection();
if(open == true)
{
string query = "select track.name as name,track.UnitPrice as price, Quantity from invoiceline " +
"left join track on invoiceline.trackid = track.trackid " +
"left join album on track.albumid = album.albumid " +
"left join artist on album.artistid = artist.artistid " +
"where invoiceid = " + invoiceId;

MySqlCommand cmd = new MySqlCommand(query, SqlConn);
MySqlDataReader reader = cmd.ExecuteReader();
while(reader.Read())
{
trackList.Add(new Track
{
TrackName = GetDBString("name", reader),
UnitPrice = float.Parse(GetDBString("price", reader)),
qty = int.Parse(GetDBString("quantity", reader))
});
}
reader.Close();
}
return trackList;
}

public List<Invoice> getInvoices(int custId)
{
@@ -193,6 +193,7 @@
<Compile Include="Controllers\GetPlaylistListController.cs" />
<Compile Include="Controllers\GetTrackController.cs" />
<Compile Include="Controllers\HomeController.cs" />
<Compile Include="Controllers\InvoiceTrackDataController.cs" />
<Compile Include="Controllers\NewCustomerController.cs" />
<Compile Include="Controllers\RemoveCustomTrackController.cs" />
<Compile Include="Controllers\RemoveTrackController.cs" />

0 comments on commit 57b6cbe

Please sign in to comment.
You can’t perform that action at this time.