public JsonResult CreateNew(Event model)
        {
            if (model.FromDate > model.ToDate)
            {
                ModelState.AddModelError("", "Event end date should be greater than event start date");
            }

            if (!model.AcSeleUserIds.Any() && !model.Visibilities.Any())
            {
                ModelState.AddModelError("", "Invite at least one user");
            }

            ModelState["FromDate"].Errors.Clear();
            ModelState["ToDate"].Errors.Clear();

            if (model.EventId != 0)
            {
                var evt = _db.Events.Find(model.EventId);
                var currUser = CurrentUser;
                if (evt.Comments[0].SenderId != currUser.UserId && !currUser.IsAdmin())
                {
                    LogUnAuth();
                }
            }

            if (ModelState.IsValid)
            {
                _srv.CreateOrUpdateEvent(model);
                return Json(new { url = "/Events/" + model.EventId });
            }

            return GetErrorMsgJSON();
        }
Example #2
0
        public Event CreateOrUpdateEvent(Event info)
        {
            info.RemoveOffset();
            List<int> newUserIds = info.AcSeleUserIds;
            var currUser = _db.Users.Find(UserSession.CurrentUserId);
            newUserIds.Add(currUser.UserId);
            newUserIds = newUserIds.Distinct().ToList();

            info.UserGroups = info.GetVisibilityCode();

            info.CreatedUserId = currUser.UserId;
            info.CreatedUserName = currUser.FullName;

            if (info.EventId == 0)
            {
                info.Invitees = CreateInvitees(newUserIds);
                _db.Events.Add(info);
                var comment = new EventComment
                {
                    SenderId = currUser.UserId,
                    SenderName = currUser.FullName,
                    Text = "Event created by " + currUser.FullName
                };
                info.Comments = new List<EventComment> { comment };
            }
            else
            {
                UpdateInvitees(info, newUserIds);
                var comment = new EventComment
                {
                    SenderId = currUser.UserId,
                    SenderName = currUser.FullName,
                    Text = info.IsDeleted
                            ? "Event was cancelled by " + currUser.FullName
                            : "Event updated by " + currUser.FullName,
                    EventId = info.EventId
                };
                _db.EventComments.Add(comment);
                _db.Entry(info).State = System.Data.EntityState.Modified;
            }
            _db.SaveChanges();
            return info;
        }
Example #3
0
 private void UpdateInvitees(Event info, IEnumerable<int> newList)
 {
     var oldInvites = _db.EventInvitees.Where(e => e.EventId == info.EventId).ToList();
     var oldList = oldInvites.Select(i => i.UserId).ToList();
     foreach (var newId in newList)
     {
         if (!oldList.Contains(newId))
         {
             var user = _db.Users.Find(newId);
             var map = new EventInvitee
             {
                 UserId = user.UserId,
                 UserName = user.FullName,
                 EventId = info.EventId
             };
             _db.EventInvitees.Add(map);
         }
     }
     foreach (var old in oldInvites)
     {
         if (!newList.Contains(old.UserId))
         {
             _db.Entry(old).State = System.Data.EntityState.Deleted;
         }
     }
     info.Invitees = null;
 }
 public JsonResult EmailNotify(Event model)
 {
     _srv.SendEmailNotification(model.EventId);
     return Json("");
 }
 public PartialViewResult New(List<int> userIds, int offset)
 {
     Event model = new Event();
     model.SetOffset(offset);
     userIds = userIds == null ? new List<int>() : userIds;
     userIds.Remove(CurrentUserId);
     var users = _db.Users.Where(u => userIds.Contains(u.UserId)).ToList();
     model.Users = users;
     ViewData[Constants.IsAdminKey] = CurrentUser.IsAdmin();
     return PartialView(model);
 }