Exemplo n.º 1
0
        /// <summary>
        /// Adding a new session to the database
        /// </summary>
        /// <param name="model">New session model</param>
        public void Insert(SessionBindingModel model)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    var session = new Session
                    {
                        DateSession     = model.DateSession,
                        SessionDuration = model.SessionDuration
                    };

                    _context.Sessions.Add(session);
                    _context.SaveChanges();

                    foreach (var userId in model.Guests)
                    {
                        _context.UserSessions.Add(new UserSession
                        {
                            SessionId = session.Id,
                            UserId    = userId
                        });
                    }
                    _context.SaveChanges();

                    transaction.Commit();
                }
                catch
                {
                    transaction.Rollback();

                    throw;
                }
            }
        }
Exemplo n.º 2
0
        public IActionResult Create([Bind("Id,DateSession,SessionDuration,Guests")] SessionBindingModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    var creator = _userStorage.GetElement(User.Identity.Name);

                    model.Guests.Add(creator.Id);
                    _sessionStorage.Insert(model);

                    return(Redirect("/Home/Index"));
                }
                catch (Exception ex)
                {
                    ModelState.AddModelError("", "Error: " + ex.Message);
                }
            }

            ViewData["GuestsId"] = new MultiSelectList(
                _userStorage.GetFullList()
                .Where(rec => rec.Email != User.Identity.Name)
                .ToList(),
                "Id",
                "Email");

            return(View(model));
        }
        public async Task <IActionResult> AddSession(SessionBindingModel sessionBindingModel)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Index"));
            }

            var speaker = await _speakerService.GetSpeakerByIdAsync(sessionBindingModel.SpeakerId);

            if (speaker == null)
            {
                return(BadRequest("Could not find the speaker"));
            }

            var sessionModel = new SessionModel
            {
                Title        = sessionBindingModel.Title,
                SpeakerModel = speaker
            };

            var successful = await _sessionService.AddSessionAsync(sessionModel);

            if (!successful)
            {
                return(BadRequest("Could not add the session."));
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 4
0
        /// <summary>
        /// Updating an existing session in the database
        /// </summary>
        /// <param name="model">New session model</param>
        public void Update(SessionBindingModel model)
        {
            using (var transaction = _context.Database.BeginTransaction())
            {
                try
                {
                    if (!IntersectionСheck(model))
                    {
                        throw new Exception("there is already a booking during this period");
                    }

                    // Updating DateSession and SessionDuration
                    var session = _context.Sessions
                                  .FirstOrDefault(rec => rec.Id == model.Id);

                    session.DateSession     = model.DateSession;
                    session.SessionDuration = model.SessionDuration;

                    _context.Sessions.Update(session);
                    _context.SaveChanges();

                    // Updating guests list
                    var sessionUsers = _context.UserSessions
                                       .Where(rec => rec.SessionId == model.Id)
                                       .ToList();

                    _context.UserSessions
                    .RemoveRange(sessionUsers);

                    _context.SaveChanges();

                    foreach (var item in model.Guests)
                    {
                        _context.UserSessions.Add(new UserSession
                        {
                            SessionId = model.Id.Value,
                            UserId    = item
                        });
                    }
                    _context.SaveChanges();

                    transaction.Commit();
                }
                catch (Exception)
                {
                    transaction.Rollback();

                    throw;
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Removing the specified session from the database by identifier
        /// </summary>
        /// <param name="model">Session model with identifier</param>
        public void Delete(SessionBindingModel model)
        {
            var element = _context.Sessions
                          .FirstOrDefault(rec => rec.Id == model.Id);

            if (element != null)
            {
                _context.Sessions.Remove(element);
                _context.SaveChanges();
            }
            else
            {
                throw new Exception("booking not found!");
            }
        }
        public async Task <IActionResult> AddSession()
        {
            var speakers = await _speakerService.GetAllSpeakersAsync();

            var speakerNames = speakers
                               .Select(x => new SelectListItem
            {
                Text  = $"{x.FirstName} {x.LastName}",
                Value = x.Id.ToString()
            }).ToList();

            var sessionBindingModel = new SessionBindingModel
            {
                SpeakerNames = speakerNames
            };

            return(View(sessionBindingModel));
        }
Exemplo n.º 7
0
        /// <summary>
        /// Checking the intersection of the specified session with other sessions
        /// </summary>
        /// <param name="model">Specified session</param>
        /// <returns>True or false</returns>
        private bool IntersectionСheck(SessionBindingModel model)
        {
            var otherSessions = _context.Sessions
                                .Where(rec => (rec.DateSession.Date == model.DateSession.Date || rec.DateSession.Date == model.DateSession.AddDays(1).Date) && rec.Id != model.Id)
                                .OrderBy(rec => rec.DateSession)
                                .ToList();

            foreach (var item in otherSessions)
            {
                if (model.DateSession < item.DateSession && model.DateSession.AddMinutes(model.SessionDuration.TotalMinutes) > item.DateSession ||
                    model.DateSession < item.DateSession.AddMinutes(item.SessionDuration.TotalMinutes) && model.DateSession.AddMinutes(model.SessionDuration.TotalMinutes) > item.DateSession.AddMinutes(item.SessionDuration.TotalMinutes) ||
                    model.DateSession > item.DateSession && model.DateSession.AddMinutes(model.SessionDuration.TotalMinutes) < item.DateSession.AddMinutes(item.SessionDuration.TotalMinutes))
                {
                    return(false);
                }
            }

            return(true);
        }
Exemplo n.º 8
0
        public IActionResult Edit(int id, [Bind("Id,DateSession,SessionDuration,Guests")] SessionBindingModel model)
        {
            if (id != model.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var creator = _userStorage.GetElement(User.Identity.Name);

                    model.Guests.Add(creator.Id);

                    _sessionStorage.Update(model);

                    return(Redirect("/Session/Index"));
                }
                catch (Exception ex)
                {
                    if (!SessionExists(model.Id.Value))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        ModelState.AddModelError("", "Error: " + ex.Message);
                    }
                }
            }

            ViewData["GuestsId"] = new MultiSelectList(
                _userStorage.GetFullList()
                .Where(rec => rec.Email != User.Identity.Name)
                .ToList(),
                "Id",
                "Email");

            return(View(model));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Search for a session by ID
        /// </summary>
        /// <param name="model">Model with ID</param>
        /// <returns>Session</returns>
        public SessionViewModel GetElement(SessionBindingModel model)
        {
            var session = _context.Sessions
                          .Include(rec => rec.UserSessions)
                          .ThenInclude(rec => rec.User)
                          .FirstOrDefault(rec => rec.Id == model.Id);

            return(session != null ?
                   new SessionViewModel
            {
                Id = session.Id,
                DateSession = session.DateSession,
                SessionDuration = session.SessionDuration,
                Guests = session.UserSessions
                         .Where(userSession => userSession.SessionId == session.Id)
                         .Select(userSession => new UserViewModel
                {
                    Id = _context.Users.FirstOrDefault(user => user.Id == userSession.UserId).Id,
                    Email = _context.Users.FirstOrDefault(user => user.Id == userSession.UserId).Email
                })
                         .ToList()
            } :
                   null);
        }