private void CreateNewInvoice()
        {
            using (var context = new ChinookEntities())
            {
                var newInvoice = new Invoice()
                {
                    BillingCountry    = Customer.Country,
                    BillingCity       = Customer.City,
                    CustomerId        = Customer.CustomerId,
                    BillingAddress    = Customer.Address,
                    BillingPostalCode = Customer.PostalCode,
                    BillingState      = Customer.State,
                    InvoiceDate       = DateTime.Now,
                };

                context.Invoices.Add(newInvoice);
                context.SaveChanges();

                foreach (var line in InvoiceLines)
                {
                    line.Invoice = newInvoice;
                    context.InvoiceLines.Add(line);
                }

                newInvoice.Total = newInvoice.InvoiceLines.Sum(i => i.UnitPrice * i.Quantity);
                context.SaveChanges();
            }
        }
Beispiel #2
0
        public void TestUpdateCustomer()
        {
            Customer customer = _CustomerRepo.GetCustomerByFirstAndLastName("Jean", "Bon");

            customer.Address = "Beaulieu";
            _ChinookEntities.SaveChanges();
        }
Beispiel #3
0
        public ActionResult Delete(int id)
        {
            Album album = ctx.Albums.Single(t => t.AlbumId == id);

            ctx.Albums.Remove(album);
            ctx.SaveChanges();
            return(RedirectToAction("Index"));
        }
        public ActionResult Create([Bind(Include = "GenreId,Name")] Genre genre)
        {
            if (ModelState.IsValid)
            {
                db.Genre.Add(genre);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(genre));
        }
        public ActionResult Create([Bind(Include = "MediaTypeId,Name")] MediaType mediaType)
        {
            if (ModelState.IsValid)
            {
                db.MediaTypes.Add(mediaType);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(mediaType));
        }
        public ActionResult Create([Bind(Include = "PlaylistId,Name")] Playlist playlist)
        {
            if (ModelState.IsValid)
            {
                db.Playlists.Add(playlist);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(playlist));
        }
        public ActionResult Create([Bind(Include = "GenreId,TrackId,TotalTrackCharge,TimeCreated")] InvoiceStatistic invoiceStatistic)
        {
            if (ModelState.IsValid)
            {
                db.InvoiceStatistics.Add(invoiceStatistic);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(invoiceStatistic));
        }
Beispiel #8
0
        public ActionResult Create([Bind(Include = "ArtistId,Name")] Artist artist)
        {
            if (ModelState.IsValid)
            {
                db.Artist.Add(artist);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            return(View(artist));
        }
        public ActionResult Create([Bind(Include = "EmployeeId,LastName,FirstName,Title,ReportsTo,BirthDate,HireDate,Address,City,State,Country,PostalCode,Phone,Fax,Email")] Employee employee)
        {
            if (ModelState.IsValid)
            {
                db.Employees.Add(employee);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ReportsTo = new SelectList(db.Employees, "EmployeeId", "LastName", employee.ReportsTo);
            return(View(employee));
        }
        public ActionResult Create([Bind(Include = "InvoiceId,CustomerId,InvoiceDate,BillingAddress,BillingCity,BillingState,BillingCountry,BillingPostalCode,Total")] Invoice invoice)
        {
            if (ModelState.IsValid)
            {
                db.Invoice.Add(invoice);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.CustomerId = new SelectList(db.Customer, "CustomerId", "FirstName", invoice.CustomerId);
            return(View(invoice));
        }
Beispiel #11
0
        public ActionResult Create([Bind(Include = "AlbumId,Title,ArtistId")] Album album)
        {
            if (ModelState.IsValid)
            {
                db.Albums.Add(album);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.ArtistId = new SelectList(db.Artists, "ArtistId", "Name", album.ArtistId);
            return(View(album));
        }
Beispiel #12
0
        public ActionResult Create([Bind(Include = "CustomerId,FirstName,LastName,Company,Address,City,State,Country,PostalCode,Phone,Fax,Email,SupportRepId")] Customer customer)
        {
            if (ModelState.IsValid)
            {
                db.Customer.Add(customer);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.SupportRepId = new SelectList(db.Employee, "EmployeeId", "LastName", customer.SupportRepId);
            return(View(customer));
        }
        public ActionResult Create([Bind(Include = "InvoiceLineId,InvoiceId,TrackId,UnitPrice,Quantity")] InvoiceLine invoiceLine)
        {
            if (ModelState.IsValid)
            {
                db.InvoiceLine.Add(invoiceLine);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.InvoiceId = new SelectList(db.Invoice, "InvoiceId", "BillingAddress", invoiceLine.InvoiceId);
            ViewBag.TrackId   = new SelectList(db.Track, "TrackId", "Name", invoiceLine.TrackId);
            return(View(invoiceLine));
        }
Beispiel #14
0
        public ActionResult Create([Bind(Include = "TrackId,Name,AlbumId,MediaTypeId,GenreId,Composer,Milliseconds,Bytes,UnitPrice")] Track track)
        {
            if (ModelState.IsValid)
            {
                db.Track.Add(track);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AlbumId     = new SelectList(db.Album, "AlbumId", "Title", track.AlbumId);
            ViewBag.GenreId     = new SelectList(db.Genre, "GenreId", "Name", track.GenreId);
            ViewBag.MediaTypeId = new SelectList(db.MediaType, "MediaTypeId", "Name", track.MediaTypeId);
            return(View(track));
        }
Beispiel #15
0
        public ActionResult Create(Track track)
        {
            if (ModelState.IsValid)
            {
                db.Tracks.Add(track);
                db.SaveChanges();
                return(RedirectToAction("Index"));
            }

            ViewBag.AlbumId     = new SelectList(db.Albums, "AlbumId", "Title", track.AlbumId);
            ViewBag.GenreId     = new SelectList(db.Genres, "GenreId", "Name", track.GenreId);
            ViewBag.MediaTypeId = new SelectList(db.MediaTypes, "MediaTypeId", "Name", track.MediaTypeId);
            return(View(track));
        }
Beispiel #16
0
        private void btn_Add_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            if (String.IsNullOrEmpty(txt_AuthorName.Text))
            {
                errorProvider.SetError(txt_AuthorName, "Enter author name");
            }
            else
            {
                using (var context = new ChinookEntities())
                {
                    var newArtist = new Artist()
                    {
                        Name = txt_AuthorName.Text
                    };

                    if (!context.Artists.Any(a => a.Name == newArtist.Name))
                    {
                        context.Artists.Add(newArtist);
                    }
                    context.SaveChanges();
                }


                this.Hide();
                var form = new AddItemForm();
                form.ShowDialog();
                this.Close();
            }
        }
        private void btn_Add_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(txt_Type.Text))
            {
                errorProvider.SetError(txt_Type, "Enter type");
            }
            else
            {
                using (var context = new ChinookEntities())
                {
                    var newType = new MediaType()
                    {
                        Name = txt_Type.Text
                    };

                    if (!context.MediaTypes.Any(t => t.Name == newType.Name))
                    {
                        context.MediaTypes.Add(newType);
                    }

                    context.SaveChanges();
                }


                this.Hide();
                var form = new AddItemForm();
                form.ShowDialog();
                this.Close();
            }
        }
Beispiel #18
0
        // PUT api/<controller>/5
        public HttpResponseMessage Put(int id, TrackDTO track)
        {
            HttpResponseMessage responseMessage;

            using (var context = new ChinookEntities())
            {
                var oldTrack = context.Tracks.Find(id);
                if (oldTrack != null)
                {
                    int newGenreId = context.Genres.Where(g => g.Name == track.Genre).SingleOrDefault().GenreId;
                    oldTrack.Name         = track.Name;
                    oldTrack.GenreId      = newGenreId;
                    oldTrack.Milliseconds = track.Milliseconds;
                    oldTrack.UnitPrice    = track.UnitPrice;

                    try
                    {
                        context.SaveChanges();
                        responseMessage = Request.CreateResponse <TrackDTO>(HttpStatusCode.OK, track);
                    }
                    catch (Exception ex)
                    {
                        responseMessage = Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                      new HttpError("Track wasn't updated!"));
                    }
                }
                else
                {
                    responseMessage = Request.CreateErrorResponse(HttpStatusCode.BadRequest,
                                                                  new HttpError("Track wasn't found!"));
                }
            }

            return(responseMessage);
        }
Beispiel #19
0
 public ActionResult Delete(int id)
 {
     using (var ctx = new ChinookEntities())
     {
         var artist = ctx.Artists.Single(t => t.ArtistId == id);
         ctx.Artists.Remove(artist);
         ctx.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
Beispiel #20
0
 public ActionResult Edit(Artist artist)
 {
     using (var ctx = new ChinookEntities())
     {
         var a = ctx.Artists.Single(t => t.ArtistId == artist.ArtistId);
         a.Name = artist.Name;
         ctx.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
Beispiel #21
0
        public void TestDeleteInvoicesLineAndRollBackJeanBon()
        {
            using (var trans = _ChinookEntities.Database.BeginTransaction())
            {
                var customer = _CustomerRepo.GetCustomerByFirstAndLastName("Jean", "Bon");

                var invoice = _InvoiceRepo.GetListInvoicesByIdCustomer(customer.CustomerId).FirstOrDefault();

                var listInvoicelines = invoice.InvoiceLine.Where(x => x.Track.Composer == "AC/DC").ToList();

                _InvoiceRepo.RemoveListInvoicesLine(listInvoicelines);

                invoice.Total = invoice.InvoiceLine.Sum(x => x.Quantity * x.UnitPrice);

                _ChinookEntities.SaveChanges();

                trans.Rollback();
            }
        }
Beispiel #22
0
        public ActionResult DeleteArtist(int id)
        {
            var findArtist = db.Artists.Find(id);

            db.Artists.Remove(findArtist);
            db.SaveChanges();
            var getArtist = db.Artists.Select(x => new { x.ArtistId, x.Name }).ToList();

            return(Json(getArtist, JsonRequestBehavior.AllowGet));
        }
Beispiel #23
0
 public ActionResult Create(Artist artist)
 {
     using (var ctx = new ChinookEntities())
     {
         var id = ctx.Artists.OrderByDescending(t => t.ArtistId).First().ArtistId;
         artist.ArtistId = ++id;
         ctx.Artists.Add(artist);
         ctx.SaveChanges();
         return(RedirectToAction("Index"));
     }
 }
Beispiel #24
0
        public void TestAjoutSupprEmployeeAvecRepository()
        {
            Employee empl = new Employee()
            {
                FirstName = "Miriam",
                LastName  = "LEFEVRE",
                Country   = "France",
                Title     = "Gestionnaire"
            };

            _EmployeeRepo.Add(empl);
            _ChinookEntities.SaveChanges();

            //On mémorise l'ID
            int id = empl.EmployeeId;

            // Pour s'assurer que l'employé a bien été ajouté en BDD, on réinstancie le contexte pour faire une lecture en BDD
            _ChinookEntities = new ChinookEntities();
            _EmployeeRepo    = new EmployeeRepository(_ChinookEntities);

            Employee employee = _EmployeeRepo.GetEmployee(id);

            //On test pour savoir si on récupère bien un employé et si son nom est bien LEFEVRE
            // On utilise la méthode Assert, le TU n'est pas validé si la condition n'est pas remplie
            Assert.IsTrue(employee?.LastName == "LEFEVRE");

            _ChinookEntities = new ChinookEntities();
            _EmployeeRepo    = new EmployeeRepository(_ChinookEntities);

            //On supprime l'entité en BDD pour revenir dans l'état initial
            _EmployeeRepo.Delete(empl);

            _ChinookEntities.SaveChanges();

            _ChinookEntities = new ChinookEntities();
            _EmployeeRepo    = new EmployeeRepository(_ChinookEntities);

            Employee employeeDeleted = _EmployeeRepo.GetEmployee(id);

            Assert.IsTrue(employeeDeleted == null);
        }
Beispiel #25
0
        public void AddAlbumWithTracks()
        {
            Artist newArtist = new Artist();

            newArtist.Name = "Rammstein";

            Album newAlbum = new Album();

            newAlbum.Title  = "RAMMSTEIN";
            newAlbum.Artist = newArtist;

            Track t1 = new Track();

            t1.Album        = newAlbum;
            t1.Name         = "DEUTSCHLAND";
            t1.MediaTypeId  = 1;
            t1.Milliseconds = 42;
            t1.UnitPrice    = new decimal(5.5);

            var persisted = _context.Track.Add(t1);

            _context.SaveChanges();
            Console.WriteLine(persisted);
        }
        // PUT: odata/Tracks(5)
        public IHttpActionResult Put([FromODataUri] int key, Delta <Track> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Track track = db.Tracks.Find(key);

            if (track == null)
            {
                return(NotFound());
            }

            patch.Put(track);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!TrackExists(key))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Updated(track));
        }
Beispiel #27
0
        // PUT: odata/Albums(5)
        public IHttpActionResult Put([FromODataUri] int key, Delta <Album> patch)
        {
            Validate(patch.GetEntity());

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            Album album = db.Albums.Find(key);

            if (album == null)
            {
                return(NotFound());
            }

            patch.Put(album);

            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!AlbumExists(key))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(Updated(album));
        }
 private Customer AddNewCustomer()
 {
     using (var context = new ChinookEntities())
     {
         if (IsInDatabase(Customer))
         {
             context.Customers.Add(Customer);
             context.SaveChanges();
             return(Customer);
         }
         else
         {
             return(context.Customers
                    .Where(c => c.FirstName == Customer.FirstName &&
                           c.LastName == Customer.LastName &&
                           c.Address == Customer.Address &&
                           c.Country == Customer.Country &&
                           c.City == Customer.City)
                    .FirstOrDefault());
         }
     }
 }
Beispiel #29
0
        private void btn_Add_Click(object sender, EventArgs e)
        {
            errorProvider.Clear();
            if (comboBox_Author.SelectedIndex < 0)
            {
                errorProvider.SetError(comboBox_Author, "Choose author");
            }
            else if (String.IsNullOrEmpty(txt_Title.Text))
            {
                errorProvider.SetError(txt_Title, "Enter album title");
            }
            else
            {
                using (var context = new ChinookEntities())
                {
                    string selectedArtist = comboBox_Author.SelectedItem.ToString();

                    var artist = context.Artists
                                 .Where(a => a.Name == selectedArtist)
                                 .FirstOrDefault();

                    var newAlbum = new Album()
                    {
                        Artist   = artist,
                        ArtistId = artist.ArtistId,
                        Title    = txt_Title.Text
                    };

                    context.Albums.Add(newAlbum);
                    context.SaveChanges();
                }
                this.Hide();
                var form = new AddItemForm();
                form.ShowDialog();
                this.Close();
            }
        }
Beispiel #30
0
 public void Commit()
 {
     ctx.SaveChanges();
 }