public StrongJsonResult <ViewDataModel> GetViewData(int?userId) { using (var db = new MyCalendarDbContext()) { var users = db.Users .Where(u => u.UserId == userId || u.UserLinksTo .Any(l => l.FromUserId == userId)) .ToList(); var data = users .Select(u => new { User = u, Events = u.Events }) .ToList(); var viewModels = data .Select(d => new UserViewModel(d.User, d.Events)) .ToList(); var model = new ViewDataModel { Users = viewModels, CurrentUserId = userId }; return(StrongJsonResult.From(model)); } }
public StrongJsonResult <UserViewModel> LinkUserToUserByEmail(int?fromUserId, string toEmail) { using (var db = new MyCalendarDbContext()) { var fromUser = db.Users.FirstOrDefault(u => u.UserId == fromUserId); if (fromUser != null) { var toUser = db.Users.FirstOrDefault(u => u.Email == toEmail && u.UserId != fromUser.UserId); if (toUser != null) { var link = db.UserLinks.FirstOrDefault(l => l.FromUserId == fromUserId && l.ToUserId == toUser.UserId); if (link == null) { link = new UserLink(fromUserId, toUser.UserId); db.UserLinks.Add(link); db.SaveChanges(); return(StrongJsonResult.From(new UserViewModel(toUser, toUser.Events))); } } } return(null); } }
public StrongJsonResult <int?> Login(LoginModel model) { using (var db = new MyCalendarDbContext()) { var user = db.Users.FirstOrDefault(u => u.Email == model.Email && u.Password == model.Password); return(StrongJsonResult.From(user?.UserId)); } }
public StrongJsonResult <int?> RemoveEvent(int?eventId) { using (var db = new MyCalendarDbContext()) { var evnt = db.Events.FirstOrDefault(e => e.EventId == eventId); db.Events.Remove(evnt); db.SaveChanges(); return(StrongJsonResult.From(eventId)); } }
public StrongJsonResult <EventViewModel> AddEvent(EventViewModel model) { using (var db = new MyCalendarDbContext()) { var evnt = new Event(); model.WriteTo(evnt); db.Events.Add(evnt); db.SaveChanges(); return(StrongJsonResult.From(new EventViewModel(evnt))); } }
public StrongJsonResult <int?> UpdateUser(UserDetailsModel userModel) { using (var db = new MyCalendarDbContext()) { var user = db.Users.FirstOrDefault(u => u.UserId == userModel.UserId); if (user != null) { userModel.WriteTo(user); db.SaveChanges(); } return(StrongJsonResult.From(user?.UserId)); } }
public StrongJsonResult <int?> UnlinkUserFromUser(int?fromUserId, int?toUserId) { using (var db = new MyCalendarDbContext()) { var link = db.UserLinks.FirstOrDefault(l => l.FromUserId == fromUserId && l.ToUserId == toUserId); if (link != null) { db.UserLinks.Remove(link); db.SaveChanges(); return(StrongJsonResult.From(link.UserLinkId)); } return(null); } }
public StrongJsonResult <int?> Register(RegisterModel model) { using (var db = new MyCalendarDbContext()) { var user = new User { Name = model.Name, Email = model.Email, Password = model.Password }; db.Users.Add(user); db.SaveChanges(); return(StrongJsonResult.From(user.UserId)); } }