// GET: Messages/Details/5 read a message, makes message read
        public async Task <IActionResult> Details(int?id)
        {
            if (id == null)
            {
                return(NotFound());
            }

            var message = await _context.Messages
                          .FirstOrDefaultAsync(m => m.id == id);

            if (message == null)
            {
                return(NotFound());
            }
            MessageViewModel vm = new MessageViewModel(); //convert message to messageVM

            vm.id       = message.id;
            vm.Reciever = CommunityUser.getEmail(message.Reciever, _context2);
            vm.Sender   = CommunityUser.getEmail(message.Sender, _context2);
            vm.Title    = message.Title;
            vm.text     = message.text;
            vm.Read     = message.Read;
            vm.TimeSent = message.TimeSent;

            return(await Details(id, vm));
        }
        public async Task <IActionResult> Create([Bind("Reciever,Title,text")] Message message)
        {
            List <string> users = CommunityUser.getEmails(_context2);

            ViewData["users"] = users;
            try    //creates new message, gets receivers id and send back confirmation
            {
                message.Reciever = _context2.Users.Single(u => u.Email == message.Reciever).Id;
                var userId = _userManager.GetUserId(User);
                message.Sender   = userId;
                message.TimeSent = DateTime.Now;
                message.Read     = false;
                _context.Add(message);
                await _context.SaveChangesAsync();

                ViewData["Confirmation"] = "Message sent to " + CommunityUser.getEmail(message.Reciever, _context2) + ", " + DateTime.Now;
                ViewData["users"]        = users;
                return(View(message));
            }
            catch (InvalidOperationException e)
            {
                ViewData["users"]        = users;
                ViewData["Confirmation"] = "Failed to deliver";
                return(View(message));
            }

            return(View(message));
        }
        // GET: Messages, gets all senders of messages to receiver
        public async Task <IActionResult> Read()
        {
            var userId = _userManager.GetUserId(User);

            List <string> messages         = Message.getIdOfSendersToReciever(userId, _context);
            List <string> emailsOfMessages = new List <string>();

            foreach (var item in messages) // all emails of senders
            {
                emailsOfMessages.Add(CommunityUser.getEmail(item, _context2));
            }
            ViewData["senders"]           = emailsOfMessages;
            ViewData["totalNrOfRead"]     = await Message.getNrOfReadMessagesAsync(userId, _context);;
            ViewData["totalNrOfMessages"] = await Message.getNrOfMessagesAsync(userId, _context);

            ViewData["totalNrOfDeleted"] = CommunityUser.getNrOfDeletedMessages(userId, _context2);
            return(View());
        }