Exemple #1
0
        public IActionResult CreateCinema([FromBody] CinemaUpsertDto cinemaDto)
        {
            if (cinemaDto == null)
            {
                return(BadRequest(ModelState));
            }

            if (cinemaRepository.CinemaExists(cinemaDto.CinemaName))
            {
                ModelState.AddModelError("", "Kino vec postoji");
                return(StatusCode(404, ModelState));
            }

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

            var obj = mapper.Map <Cinema>(cinemaDto);

            if (!cinemaRepository.CreateCinema(obj))
            {
                ModelState.AddModelError("", $"Nesto je pogresno u spremanju {obj.CinemaName}");
                return(StatusCode(500, ModelState));
            }

            return(CreatedAtRoute("GetCinema", new { id = obj.CinemaId }, obj));
        }
        public async Task <IActionResult> PutCinema(int id, Cinema cinema)
        {
            User user = await _context.Users.FindAsync(User.Claims.ToList()[0].Value);

            if (user == null)
            {
                return(Unauthorized());
            }

            Cinema dbCinema = _cinemaRepository.GetCinema(User, id);

            if (dbCinema == null)
            {
                return(NotFound("Cinema: " + id + " not found"));
            }

            if (id != cinema.Id)
            {
                return(BadRequest());
            }

            _context.Entry(cinema).State = EntityState.Modified;

            try
            {
                await _context.SaveChangesAsync();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!_cinemaRepository.CinemaExists(id))
                {
                    return(NotFound());
                }
                else
                {
                    throw;
                }
            }

            return(NoContent());
        }