示例#1
0
        public async Task <IActionResult> PostSession([FromBody] Sessionn session)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }
            var      claims    = User.Claims;
            var      cla       = claims.ToList();
            var      idClient  = int.Parse(cla[1].Value);
            DateTime localDate = DateTime.Now;

            session.FkClientidClient = idClient;

            if (localDate > session.TimeStart || localDate > session.TimeEnd)
            {
                return(BadRequest("time is already over"));
            }
            if (session.TimeStart >= session.TimeEnd)
            {
                return(BadRequest("end time is bigger then start time"));
            }
            var FreetimesExist = _context.FreeTime.Where(l => l.TimeStart <= session.TimeStart && l.TimeEnd >= session.TimeEnd && l.TimeEnd > session.TimeStart && l.IdFreeTime == session.FkFreeTimeidFreeTime)
                                 .Select(x => x.IdFreeTime).Count();

            if (FreetimesExist < 1)
            {
                return(BadRequest("here is no time in freeTime"));
            }


            var alreadyis = _context.Sessionn.Where(l => ((l.TimeStart >= session.TimeStart && l.TimeEnd >= session.TimeEnd && session.TimeEnd >= l.TimeStart) ||
                                                          (l.TimeStart <= session.TimeStart && l.TimeEnd >= session.TimeEnd) ||
                                                          (l.TimeStart <= session.TimeStart && l.TimeEnd >= session.TimeStart && l.TimeEnd <= session.TimeEnd) ||
                                                          (l.TimeStart >= session.TimeStart && l.TimeEnd <= session.TimeEnd) ||
                                                          (l.TimeStart == session.TimeStart && l.TimeEnd == session.TimeEnd)) && l.FkFreeTimeidFreeTime == session.FkFreeTimeidFreeTime)
                            .Select(x => x.IdSession).Count();


            if (alreadyis > 0)
            {
                return(BadRequest("time is already busy"));
            }

            _context.Sessionn.Add(session);
            await _context.SaveChangesAsync();

            return(CreatedAtAction("GetSession", new { id = session.IdSession }, session));
        }
示例#2
0
        public async Task <IActionResult> PutSession([FromRoute] int id, [FromBody] Sessionn session)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }


            var claims   = User.Claims;
            var cla      = claims.ToList();
            var idClient = int.Parse(cla[1].Value);
            var check    = _context.Sessionn.Where(l => (l.IdSession == id && l.FkClientidClient == idClient)).Select(x => x.IdSession).Count();

            if (check < 1)
            {
                return(Unauthorized());
            }

            var FreetimesExist = _context.FreeTime.Where(l => l.TimeStart <= session.TimeStart && l.TimeEnd >= session.TimeEnd && l.TimeEnd > session.TimeStart && l.IdFreeTime == session.FkFreeTimeidFreeTime)
                                 .Select(x => x.IdFreeTime).Count();

            if (FreetimesExist < 1)
            {
                return(BadRequest("here is no time in freeTime"));
            }


            var alreadyis = _context.Sessionn.Where(l => ((l.TimeStart >= session.TimeStart && l.TimeEnd >= session.TimeEnd && session.TimeEnd >= l.TimeStart) ||
                                                          (l.TimeStart <= session.TimeStart && l.TimeEnd >= session.TimeEnd) ||
                                                          (l.TimeStart <= session.TimeStart && l.TimeEnd >= session.TimeStart && l.TimeEnd <= session.TimeEnd) ||
                                                          (l.TimeStart >= session.TimeStart && l.TimeEnd <= session.TimeEnd) ||
                                                          (l.TimeStart == session.TimeStart && l.TimeEnd == session.TimeEnd)) && l.FkFreeTimeidFreeTime == session.FkFreeTimeidFreeTime)
                            .Select(x => x.IdSession).Count();


            if (alreadyis > 1)
            {
                return(BadRequest("time is already busy"));
            }



            if (id != session.IdSession)
            {
                return(BadRequest());
            }

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

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

            return(NoContent());
        }