public async Task <IActionResult> AddPiece(Piece piece) { if (ModelState.IsValid) { _context.Piece.Add(piece); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(piece)); }
public async Task <IActionResult> AddScore(Score score) { if (ModelState.IsValid) { _context.Score.Add(score); await _context.SaveChangesAsync(); return(RedirectToAction("AddPieces", "ScorePieces", new { id = score.ScoreId })); } return(View(score)); }
//Creates new ScorePiece object to add pieces with the corresponding scoreId //Saves to the database //Add Model State checking public async Task <IActionResult> AddPieces(string id) { int sid = Convert.ToInt32(id); ScorePieces scorepiece = new ScorePieces() { ScoreId = sid }; _context.ScorePieces.Add(scorepiece); await _context.SaveChangesAsync(); return(View(scorepiece)); }
//POST: Checkin/Confirm //Does the logic for checking in a piece and updating the condition //it is in. Returns a confirmation page. public async Task <IActionResult> Confirm() { //Find the id of the musician whose piece is being checked in. int musicianId = Convert.ToInt32(Request.Form["musician"]); //Find every piece that is being checked in. List <int> toReturn = new List <int>(); IEnumerable <String> keys = Request.Form.Keys; foreach (String k in keys) { Match m = Regex.Match(k, "Piece [0-9][0-9]*"); if (m.Success) { toReturn.Add(Convert.ToInt32(k.Substring(5))); continue; } } //List<Piece> pieces = new List<Piece>(); //List<String> condition = new List<string>(); //For each piece being returned, assess the condition and update the rating. foreach (int i in toReturn) { String cond = Request.Form["condition " + i]; Return(i, musicianId, cond); } await _context.SaveChangesAsync(); return(View()); }
//GET: Musician/AddTo //Takes a musician and an ensemble by id, and add the musician as a player in the ensemble. //Then returns to the index page with the ensemble being passed. public async Task <IActionResult> AddTo(int mus, int ens) { Musician m = _context.Musician.Find(mus); Ensemble e = _context.Ensemble.Find(ens); if (m == null || e == null) { return(NotFound()); } EnsemblePlayers ep = _context.EnsemblePlayers.Find(ens, mus); if (ep != null) { return(RedirectToAction("AlreadyIn", new { mus = m.MusicianName, ens = e.EnsembleName })); } EnsemblePlayers ensemblePlayer = new EnsemblePlayers { EnsembleId = ens, MusicianId = mus }; await _context.EnsemblePlayers.AddAsync(ensemblePlayer); await _context.SaveChangesAsync(); return(RedirectToAction("Index", new { id = ens })); }
// GET: Ensemble/RemoveFrom //Takes an ensemble and musician by id, and removes the musician from //the ensemble. public async Task <IActionResult> RemoveFrom(int ens, int mus) { EnsemblePlayers ensemblePlayer; try { ensemblePlayer = _context.EnsemblePlayers.First(e => e.EnsembleId == ens && e.MusicianId == mus); } catch { return(NotFound()); } _context.Remove(ensemblePlayer); await _context.SaveChangesAsync(); return(RedirectToAction("Musicians", new { id = ens })); }
// GET: Cart/Confirm // Officially checks out the elements of the cart to the selected ensemble, // creating a new record of a piece being checked out. public async Task <IActionResult> CheckOut() { IEnumerable <string> keys = Request.Form.Keys; List <CheckedOut> toCheckOut = new List <CheckedOut>(); Dictionary <int, int> piecesAvailable = new Dictionary <int, int>(); Dictionary <int, int> piecesToCheckOut = new Dictionary <int, int>(); //Go through every musician we are checking out to, and add //any piece that we have selected for them to check out. //If availablie, check out the piece. System.Diagnostics.Debug.WriteLine("\n\n"); foreach (String s in keys) { string regex = "musician [0-9][0-9]* score [0-9][0-9]*"; Match m = Regex.Match(s, regex); //Found a musician, save their piece. if (m.Success) { string[] split = s.Split(" "); int musician = Convert.ToInt32(split[1]); int pieceId = -1; String pieceStr = Request.Form[s]; System.Diagnostics.Debug.WriteLine("piece " + pieceStr); if (pieceStr == null || pieceStr.Equals("Nothing")) { continue; } pieceId = Convert.ToInt32(pieceStr); //As we find new pieces, add it to the list and calculate the //number still available to check out. Then keep track of the //number we need to perform the check out. if (!piecesAvailable.ContainsKey(pieceId)) { Piece p = _context.Piece.Find(pieceId); int available = p.NumberofParts; IEnumerable <CheckedOut> outs = from co in _context.CheckedOut where co.PartId == pieceId select co; available -= outs.Count(); piecesAvailable.Add(pieceId, available); piecesToCheckOut.Add(pieceId, 1); } else { piecesToCheckOut[pieceId]++; } //Store the record. toCheckOut.Add(new CheckedOut { MusicianId = musician, PartId = pieceId }); } } //Find any piece that we do not have enough of available to perform check out. IEnumerable <int> pieces = piecesToCheckOut.Keys; Cart.unavailable = new List <Piece>(); foreach (int piece in pieces) { if (piecesToCheckOut[piece] > piecesAvailable[piece]) { Cart.unavailable.Add(_context.Piece.Find(piece)); } } System.Diagnostics.Debug.WriteLine("\n\n"); //If there are any pieces we cannot check out, redirect to an //error giving a few details. if (Cart.unavailable.Any()) { return(RedirectToAction("CheckOutError")); } //Everything succeeded, officially perform the check out action. foreach (CheckedOut co in toCheckOut) { //Not great, but _context.CheckedOut.Any( ... ) threw a runtime error. IEnumerable <CheckedOut> checkedOuts = from c in _context.CheckedOut where c.MusicianId == co.MusicianId && c.PartId == co.PartId select c; if (checkedOuts.Any()) { continue; } _context.CheckedOut.Add(co); } //Empty the cart, save changes. ShoppingCart.Clear(); await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Confirm))); }