public async Task <ActionResult <Movie> > AddMovieAsync([FromBody] Movie movie)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }
            await _context.Movies.AddAsync(movie);

            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetMovie", new { id = movie.Id }, movie));
        }
        public async Task <ActionResult <CustomerDTO> > CreateCustomerAsync(CustomerDTO customerDto)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var customer = Mapper.Map <CustomerDTO, Customer>(customerDto);
            await _context.Customers.AddAsync(customer);

            await _context.SaveChangesAsync();

            customerDto.Id = customer.Id;
            return(CreatedAtAction("GetCustomer", new { id = customerDto.Id }, customerDto));
        }
示例#3
0
        public async Task <ActionResult <NoweWypozyczenie> > AddWypozyczenieAsync([FromBody] NoweWypozyczenie noweWypozyczenie)
        {
            if (noweWypozyczenie.MovieIDs.Count == 0)
            {
                return(BadRequest("Brak filmów na liście wypożyczeń"));
            }
            var customer = await _context.Customers.SingleOrDefaultAsync(c => c.Id == noweWypozyczenie.CustomerId);

            if (customer == null)
            {
                return(BadRequest("Nieprawidłowe ID klienta"));
            }

            var movies = _context.Movies.Where(m => noweWypozyczenie.MovieIDs.Contains(m.Id)).ToList();

            if (movies.Count != noweWypozyczenie.MovieIDs.Count)
            {
                return(BadRequest("Nie załadowano filmu"));
            }
            foreach (var movie in movies)
            {
                if (movie.IloscDostepnychKopi == 0)
                {
                    return(BadRequest(movie.Name + " jest niedostępny do wypożyczenia"));
                }
                movie.IloscDostepnychKopi--;
                var wypozyczenie = new Wyporzyczenia
                {
                    Customer          = customer,
                    CustomerId        = customer.Id,
                    Movie             = movie,
                    MovieId           = movie.Id,
                    DataWyporzyczenia = DateTime.Now
                };
                await _context.Wyporzyczenia.AddAsync(wypozyczenie);
            }

            await _context.SaveChangesAsync();

            return(Ok());
        }