public async Task <ActionResult <AvailableTimeWindow> > Add(AvailableTimeWindow availableTimeWindow)
        {
            if (availableTimeWindow.StartHour > availableTimeWindow.EndHour)
            {
                return(new BadRequestResult());
            }
            if (await _dataContext.AvailableTimeWindows.CountAsync(a =>
                                                                   a.OwnerId == availableTimeWindow.OwnerId && a.WeekDay == availableTimeWindow.WeekDay) > 0)
            {
                return(new ConflictResult());
            }
            await _dataContext.AvailableTimeWindows.AddAsync(availableTimeWindow);

            await _dataContext.SaveChangesAsync();

            return(new CreatedResult("", availableTimeWindow));
        }
        public async Task <ActionResult <AvailableTimeWindow> > Add([FromBody] AddDto data)
        {
            User owner;

            try
            {
                owner = await _dataContext.Users.FirstAsync(u => u.Id == data.OwnerId);
            }
            catch (InvalidOperationException)
            {
                return(new NotFoundResult());
            }

            var availableTimeWindow = new AvailableTimeWindow
            {
                Owner   = owner, OwnerId = owner.Id, StartHour = data.StartHour, EndHour = data.EndHour,
                WeekDay = data.WeekDay
            };

            return(new CreatedResult("Created", await Add(availableTimeWindow)));
        }
        public async Task <ActionResult <AvailableTimeWindow> > AddSelf([FromBody] AddDto data)
        {
            var  ownerEmail = HttpContext.User.FindFirst(c => c.Type == ClaimTypes.Email).Value;
            User owner;

            try
            {
                owner = await _dataContext.Users.FirstAsync(u => u.Email == ownerEmail);
            }
            catch (InvalidOperationException)
            {
                return(new NotFoundResult());
            }

            var availableTimeWindow = new AvailableTimeWindow
            {
                Owner   = owner, OwnerId = owner.Id, StartHour = data.StartHour, EndHour = data.EndHour,
                WeekDay = data.WeekDay
            };

            return(new CreatedResult("Created", await Add(availableTimeWindow)));
        }
        public async Task <ActionResult <AvailableTimeWindow> > SetAvailableTimeWindow(
            [FromBody] SetAvailableTimeWindowDto data)
        {
            var user = await GetUser();

            if (user == null)
            {
                return(new NotFoundResult());
            }
            AvailableTimeWindow atw;

            if (data.StartHour >= data.EndHour)
            {
                return(new BadRequestResult());
            }

            try
            {
                atw = await _dataContext.AvailableTimeWindows.FirstAsync(a =>
                                                                         a.OwnerId == user.Id && a.WeekDay == data.WeekDay);

                atw.StartHour = data.StartHour;
                atw.EndHour   = data.EndHour;
                await _dataContext.SaveChangesAsync();
            }
            catch (InvalidOperationException)
            {
                atw = new AvailableTimeWindow
                {
                    OwnerId = user.Id, StartHour = data.StartHour, EndHour = data.EndHour, WeekDay = data.WeekDay
                };
                await _dataContext.AvailableTimeWindows.AddAsync(atw);

                await _dataContext.SaveChangesAsync();
            }

            return(new ActionResult <AvailableTimeWindow>(atw));
        }