// GET: Trip/Details/5
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var tripModel = await _context.TripModel
                            .FirstOrDefaultAsync(m => m.Id == id);

            if (tripModel == null)
            {
                return(NotFound());
            }

            tripModel.Messages = _context.MessagesModel.Where(x => x.TripId == tripModel.Id).ToList();
            var subscribedUsers = _context.SubscribedModel.Where(x => x.TripId == id);
            var confirmedUsers  = subscribedUsers.Where(x => x.IsConfirmed == true);

            foreach (var user in confirmedUsers)
            {
                if (user.UserId == User.GetUserId())
                {
                    tripModel.Confirmed = true;
                    break;
                }
            }

            foreach (var user in subscribedUsers)
            {
                if (User.GetUserId() == user.UserId)
                {
                    tripModel.Subscribed = true;
                }

                var subscribed = new SubscribedUsers();
                var subbedUser = _context.Users.Where(x => x.Id == user.UserId).FirstOrDefault();

                subscribed.UserName    = subbedUser.FirstName + " " + subbedUser.LastName;
                subscribed.UserId      = user.UserId;
                subscribed.IsConfirmed = user.IsConfirmed;

                tripModel.SubscribedUsers.Add(subscribed);
            }

            return(View(tripModel));
        }
        public async Task <IActionResult> Unsubscribe(string id)
        {
            if (id == null)
            {
                return(NotFound());
            }
            SubscribedUsers user = await _context.SubscribedUsers.Where(user => user.ActiveCode == id).FirstOrDefaultAsync();

            if (user == null)
            {
                return(NotFound());
            }
            user.Actived = false;
            await _context.SaveChangesAsync();

            return(RedirectToAction("index", "home"));
        }
Esempio n. 3
0
        public async Task <IActionResult> Subscribe(SubscribedUsers sub)
        {
            if (ModelState.IsValid)
            {
                _db.SubscribedUsers.Add(sub);
                sub.ActiveCode = Guid.NewGuid().ToString();
                sub.Actived    = true;
                await _db.SaveChangesAsync();

                return(Json("Success!"));
            }
            string errors = "";

            foreach (var item in ModelState.Values)
            {
                foreach (var error in item.Errors)
                {
                    errors += error.ErrorMessage;
                }
            }
            return(Json(errors));
        }