Exemplo n.º 1
0
        public async Task <ActionResult> Post([FromBody] TrainingAddModel model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var coach = await _coachRepository.GetAsync(model.CoachId);

            if (coach == null)
            {
                return(BadRequest($"No coach was found with Id:{model.CoachId}"));
            }

            var hall = await _hallRepository.GetAsync(model.HallId);

            if (hall == null)
            {
                return(BadRequest($"No hall was found with Id:{model.HallId}"));
            }

            var dateFrom = model.StartTime.AddHours(-1);
            var dateTo   = model.StartTime.AddHours(1);

            var conflictingTraining = _repository
                                      .GetForRange(dateFrom, dateTo)
                                      .Where(t => t.CoachId == model.CoachId ||
                                             t.HallId == model.HallId);

            if (conflictingTraining.Any())
            {
                return(BadRequest($"Cannot add training for this time" +
                                  $" (Coach or hall busy for this time)"));
            }

            var entity = new Training()
            {
                Title        = model.Title,
                TrainingType = model.TrainingType,
                CoachId      = model.CoachId,
                HallId       = model.HallId,
                StartTime    = model.StartTime
            };

            // try
            //  {
            _repository.Create(entity);
            await _repository.SaveChangesAsync();

            //   }
            //    catch (Exception e)
            //   {
            //       return BadRequest();
            //    }

            return(Created(string.Empty, entity));
        }
Exemplo n.º 2
0
 public async Task <ActionResult <IEnumerable <Hall> > > Get()
 {
     return(Ok(await _repository.GetAsync()));
 }