public EventChatroomMessageViewModel(EventChatroomMessage message, ApplicationUser organizer, EventMember[] eMember)
 {
     EventChatroomMessageId = message.EventChatroomMessageId;
     Author       = new Author(organizer, eMember);
     Message      = message.Message;
     Timestamp    = message.DateCreated.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
     LastModified = message.LastModified;
 }
        // [Authorize]
        public async Task <IActionResult> Post([FromRoute] int eventId, [FromBody] CreateEventChatroomMessageViewModel message)
        {
            if (ModelState.IsValid)
            {
                var user = await GetCurrentUserAsync();

                EventMember[] eventMember = await _context.EventMember.Include(m => m.ApplicationUser)
                                            .Where(e => e.EventId == eventId && e.ApplicationUser.Id == user.Id)
                                            .ToArrayAsync();

                Event hostedEvent = await _context.Event.Include(e => e.Organization)
                                    .ThenInclude(o => o.Organizer)
                                    .Where(e => e.Organization.Organizer.Id == user.Id && e.EventId == eventId)
                                    .SingleOrDefaultAsync();

                // If the user is neither an event member nor the event organizer, forbid
                if (eventMember == null && hostedEvent == null)
                {
                    return(Forbid());
                }

                EventChatroomMessage newMessage = new EventChatroomMessage()
                {
                    Message  = message.Message,
                    Author   = user,
                    AuthorId = user.Id,
                    EventId  = eventId
                };

                await _context.AddAsync(newMessage);

                await _context.SaveChangesAsync();

                await _context.Entry(newMessage).GetDatabaseValuesAsync();

                EventChatroomMessageViewModel model = null;

                // If the user was an event member AND the organizer of the event
                if (eventMember != null && hostedEvent != null)
                {
                    model = new EventChatroomMessageViewModel(newMessage, user, eventMember);
                }
                // If the user was an event member and NOT the organizer of the event
                else if (eventMember != null && hostedEvent == null)
                {
                    model = new EventChatroomMessageViewModel(newMessage, eventMember);
                }
                // If the user was NOT an event member AND is the organizer of the event
                else if (eventMember == null && hostedEvent != null)
                {
                    model = new EventChatroomMessageViewModel(newMessage, user);
                }

                this.Clients.Group("Event" + eventId.ToString()).AddChatMessage(model);
                return(Json(model));
            }
            return(NotFound());
        }
        // [Authorize]
        public async Task <IActionResult> Patch([FromRoute] int eventId, [FromRoute] int messageId, [FromBody] EditEventChatroomMessageViewModel message)
        {
            if (ModelState.IsValid)
            {
                var user = await GetCurrentUserAsync();

                EventChatroomMessage originalMessage = await _context.EventChatroomMessage.Where(m => m.EventChatroomMessageId == messageId && m.EventId == eventId).SingleOrDefaultAsync();

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

                EventMember[] allEventMembersForAuthorOfMessage = await _context.EventMember.Where(e => e.EventId == originalMessage.EventId && e.ApplicationUser.Id == user.Id).ToArrayAsync();

                Event hostedEvent = await _context.Event.Include(e => e.Organization)
                                    .ThenInclude(o => o.Organizer)
                                    .Where(e => e.Organization.Organizer.Id == user.Id && e.EventId == originalMessage.EventId)
                                    .SingleOrDefaultAsync();

                originalMessage.Message      = message.Message;
                originalMessage.LastModified = DateTime.Now;

                _context.Entry(originalMessage).State = EntityState.Modified;
                _context.Update(originalMessage);
                await _context.SaveChangesAsync();

                await _context.Entry(originalMessage).GetDatabaseValuesAsync();

                EventChatroomMessageViewModel model = null;

                // If the user was an event member AND the organizer of the event
                if (allEventMembersForAuthorOfMessage != null && hostedEvent != null)
                {
                    model = new EventChatroomMessageViewModel(originalMessage, user, allEventMembersForAuthorOfMessage);
                }
                // If the user was an event member and NOT the organizer of the event
                else if (allEventMembersForAuthorOfMessage != null && hostedEvent == null)
                {
                    model = new EventChatroomMessageViewModel(originalMessage, allEventMembersForAuthorOfMessage);
                }
                // If the user was NOT an event member AND is the organizer of the event
                else if (allEventMembersForAuthorOfMessage == null && hostedEvent != null)
                {
                    model = new EventChatroomMessageViewModel(originalMessage, user);
                }

                model.LastModified = originalMessage.LastModified;

                this.Clients.Group("Event" + originalMessage.EventId.ToString()).EditChatMessage(model);
                return(new NoContentResult());
            }
            return(NotFound(ModelState));
        }
        public async Task <IActionResult> Delete([FromRoute] int eventId, [FromRoute] int messageId)
        {
            if (await _validateUserInEvent(eventId))
            {
                ApplicationUser user = await GetCurrentUserAsync();

                EventChatroomMessage messageToDelete = await _context.EventChatroomMessage.Where(m => m.EventChatroomMessageId == messageId && m.AuthorId == user.Id).SingleOrDefaultAsync();

                if (messageToDelete == null)
                {
                    return(NotFound());
                }
                _context.Remove(messageToDelete);
                await _context.SaveChangesAsync();

                this.Clients.Group("Event" + eventId.ToString()).DeleteChatMessage(messageId);
                return(new NoContentResult());
            }
            return(new ForbidResult());
        }