Пример #1
0
 public void AddHall(HallDto dto)
 {
     var entity = new Hall();
     entity.Name = dto.Name;
     _hallRepository.Add(entity);
     _hallRepository.Save();
 }
Пример #2
0
 public ActionResult Create(HallDto dto)
 {
     if (!ModelState.IsValid)
     {
         TempData["error"] = "Check your input.";
         return(RedirectToAction(nameof(Create)));
     }
     try
     {
         executor.ExecuteCommand(addHall, dto);
         return(RedirectToAction(nameof(Index)));
     }
     catch (EntityNotAllowedException)
     {
         return(RedirectToAction("PageNotFound", "Redirections"));
     }
     catch (EntityAlreadyExistsException e)
     {
         TempData["error"] = e.Message;
     }
     catch (Exception e)
     {
         TempData["error"] = e.Message;
     }
     return(RedirectToAction(nameof(Index)));
 }
Пример #3
0
        public HallDto GetHallById(int id)
        {
            using (SqlConnection conn = new SqlConnection(_connection.connectionString))
            {
                conn.Open();

                SqlCommand command = new SqlCommand("SELECT Id, Price, ScreenType, Seats, SeatsTaken FROM Hall WHERE Id = @Id", conn);
                command.Parameters.AddWithValue("@Id", id);
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var hall = new HallDto
                        {
                            Id         = (int)reader["Id"],
                            Price      = (decimal)reader["Price"],
                            ScreenType = reader["ScreenType"]?.ToString(),
                            Seats      = (int)reader["Seats"],
                            SeatsTaken = (int)reader["SeatsTaken"]
                        };
                        return(hall);
                    }
                }
                return(null);
            }
        }
Пример #4
0
        public List <HallDto> GetHalls()
        {
            using (SqlConnection conn = new SqlConnection(_connection.connectionString))
            {
                conn.Open();

                var        halls   = new List <HallDto>();
                SqlCommand command = new SqlCommand("SELECT Id, Price, ScreenType, Seats, SeatsTaken FROM Hall", conn);

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        var hall = new HallDto
                        {
                            Id         = (int)reader["Id"],
                            Price      = (decimal)reader["Price"],
                            ScreenType = reader["ScreenType"].ToString(),
                            Seats      = (int)reader["Seats"],
                            SeatsTaken = (int)reader["SeatsTaken"]
                        };
                        halls.Add(hall);
                    }
                }
                return(halls);
            }
        }
Пример #5
0
 public IActionResult Post([FromBody] HallDto dto)
 {
     try
     {
         addHall.Execute(dto);
         return(StatusCode(201));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(StatusCode(409, new
         {
             Errors = new List <string> {
                 e.Message
             }
         }));
     }
     catch (Exception e)
     {
         return(StatusCode(500, new
         {
             Errors = new List <string> {
                 e.Message
             }
         }));
     }
 }
Пример #6
0
        public ServiceResponse <HallDto> GetHallById(int hallId)
        {
            _logger.LogInformation($"Вход в метод HallService.GetHallById c параметром {hallId}");

            return(Execute(() =>
            {
                var hallDto = new HallDto(_hallRepository.GetHallById(hallId));

                return ServiceResponse <HallDto> .Ok(hallDto);
            }));
        }
Пример #7
0
 public ActionResult Create(HallDto hall)
 {
     try
     {
         _hallService.AddHall(hall);
         return RedirectToAction("Index");
     }
     catch
     {
         return View();
     }
 }
Пример #8
0
        public void Execute(HallDto request)
        {
            var hall = _context.Halls.Find(request.Id);

            if (hall == null)
            {
                throw new EntityNotFoundException(request.Id, typeof(Hall));
            }
            _validator.ValidateAndThrow(request);

            _mapper.Map(request, hall);

            _context.SaveChanges();
        }
Пример #9
0
        public void Execute(HallDto request)
        {
            if (Context.Halls.Any(h => h.Name.ToLower() == request.Name.ToLower()))
            {
                throw new EntityAlreadyExistsException("Hall");
            }

            Context.Halls.Add(new Hall
            {
                Name             = request.Name,
                MaximumOccupancy = request.MaximumOccupancy
            });

            Context.SaveChanges();
        }
Пример #10
0
        public void Execute(HallDto request)
        {
            var hall = Context.Halls.Find(request.Id);

            if (hall == null || hall.IsDeleted == true)
            {
                throw new EntityNotFoundException("Hall");
            }

            if (request.Name.ToLower() != hall.Name.ToLower() && Context.Halls.Any(h => h.Name.ToLower() == request.Name.ToLower()))
            {
                throw new EntityAlreadyExistsException("Hall with that name");
            }

            hall.Name             = request.Name;
            hall.MaximumOccupancy = request.MaximumOccupancy;

            Context.SaveChanges();
        }
Пример #11
0
 public ActionResult Edit(int id, HallDto dto)
 {
     try
     {
         dto.Id = id;
         executor.ExecuteCommand(editHall, dto);
         return(RedirectToAction(nameof(Index)));
     }
     catch (EntityNotAllowedException)
     {
         return(RedirectToAction("PageNotFound", "Redirections"));
     }
     catch (EntityAlreadyExistsException e)
     {
         TempData["error"] = e.Message;
     }
     catch (Exception e)
     {
         TempData["error"] = e.Message;
     }
     return(RedirectToAction(nameof(Index)));
 }
Пример #12
0
 public IActionResult Put(int id, [FromBody] HallDto dto)
 {
     try
     {
         dto.Id = id;
         editHall.Execute(dto);
         return(StatusCode(204));
     }
     catch (EntityAlreadyExistsException e)
     {
         return(StatusCode(409, new
         {
             Errors = new List <string> {
                 e.Message
             }
         }));
     }
     catch (EntityNotFoundException e)
     {
         return(NotFound(new
         {
             Errors = new List <string> {
                 e.Message
             }
         }));
     }
     catch (Exception e)
     {
         return(StatusCode(500, new
         {
             Errors = new List <string> {
                 e.Message
             }
         }));
     }
 }
Пример #13
0
 public IActionResult Put(int id, [FromBody] HallDto dto, [FromServices] IEditHallCommand command)
 {
     dto.Id = id;
     _executor.ExecuteCommand(command, dto);
     return(NoContent());
 }