public HttpResponseMessage ConfirmInvitationToEvent(string sessionKey, int eventId) { try { var context = new TheSocialEventsContext(); var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Invalid authentication!"); } var theEvent = context.UsersEvents.FirstOrDefault(e => e.EventId == eventId && e.UserId == user.Id); if(theEvent==null) { throw new ArgumentException("You are not invited to this event!"); } theEvent.State=State.Confirmed; context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage CreateEvent([FromBody]CreateEventModel newEvent, string sessionKey) { try { var context = new TheSocialEventsContext(); var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Invalid authentication!"); } var eventToAdd = new MasterEvent() { Name = newEvent.Name, Description = newEvent.Description, Address = newEvent.Address, Creator = user }; context.MasterEvents.Add(eventToAdd); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created); return response; } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage AddFriend(string sessionKey, string friendEmail) { try { var context = new TheSocialEventsContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Invalid user authentication."); } var friend = context.Users.FirstOrDefault(u => u.Email == friendEmail); if(friend==null) { throw new ArgumentException("There is no user with such email"); } user.Friends.Add(friend); context.SaveChanges(); var response = Request.CreateResponse(HttpStatusCode.OK, friend); return response; } } catch (Exception ex) { var response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage CreateEvent([FromBody]CreateEventModel newEvent, string sessionKey) { try { var context = new TheSocialEventsContext(); var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Invalid authentication!"); } var eventToAdd = new Event() { Name = newEvent.Name, Description = newEvent.Description, Address = newEvent.Address, Creator = user }; context.Events.Add(eventToAdd); if (newEvent.InvitedUsersEmails != null) { var invitedUsers = context.Users.Where(u => newEvent.InvitedUsersEmails.Contains(u.Email)).ToList(); if (invitedUsers != null) { foreach (var invitedUser in invitedUsers) { context.UsersEvents.Add(new UsersEvents() { User = invitedUser, Event = eventToAdd, State = State.NotAnswered }); } } } context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.Created); return response; } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage DeclineInvitationToEvent(string sessionKey, int eventId, CommentModel comment) { try { var context = new TheSocialEventsContext(); var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Invalid authentication!"); } var theEvent = context.Events.FirstOrDefault(e => e.Id == eventId); if (theEvent == null) { throw new ArgumentException("There is no such event!"); } theEvent.Comments.Add(new Comment() { Text = comment.Text, User = user }); context.SaveChanges(); var response = this.Request.CreateResponse(HttpStatusCode.OK); return response; } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage EditProfile(string sessionKey, UserModel newUser) { try { var context = new TheSocialEventsContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Invalid authentication!"); } if(newUser.FullName!=null && newUser.FullName.Length>=6) { user.FullName = newUser.FullName; } if(newUser.PictureUrl!=null) { user.PictureUrl = newUser.PictureUrl; } context.SaveChanges(); var response = Request.CreateResponse(HttpStatusCode.OK); return response; } } catch (Exception ex) { var response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage PutLogoutUser(string sessionKey) { try { var context = new TheSocialEventsContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.SessionKey == sessionKey); if (user == null) { throw new ArgumentException("Invalid user authentication."); } user.SessionKey = null; context.SaveChanges(); var response = Request.CreateResponse(HttpStatusCode.OK); return response; } } catch (Exception ex) { var response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage PostRegisterUser(UserModel model) { try { var dbContext = new TheSocialEventsContext(); using (dbContext) { this.ValidateEmail(model.Email); this.ValidateName(model.FullName); var user = dbContext.Users.FirstOrDefault(u => u.Email == model.Email); if (user != null) { throw new InvalidOperationException("Users exists"); } user = new User() { Email = model.Email, FullName = model.FullName, AuthCode = model.AuthCode, PictureUrl = model.PictureUrl }; dbContext.Users.Add(user); dbContext.SaveChanges(); if (user.SessionKey == null) { user.SessionKey = this.GenerateSessionKey(user.Id); dbContext.SaveChanges(); } user.SessionKey = this.GenerateSessionKey(user.Id); dbContext.SaveChanges(); var loggedModel = new LoggedUserModel() { FullName = user.FullName, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }
public HttpResponseMessage PostLoginUser(UserModel model) { try { ValidateEmail(model.Email); var context = new TheSocialEventsContext(); using (context) { var user = context.Users.FirstOrDefault(u => u.Email == model.Email && u.AuthCode == model.AuthCode); if (user == null) { throw new InvalidOperationException("Invalid username or password"); } if (user.SessionKey == null) { user.SessionKey = this.GenerateSessionKey(user.Id); context.SaveChanges(); } var loggedModel = new LoggedUserModel() { FullName = user.FullName, SessionKey = user.SessionKey }; var response = this.Request.CreateResponse(HttpStatusCode.Created, loggedModel); return response; } } catch (Exception ex) { var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message); return response; } }