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;
            }
        }
Ejemplo n.º 2
0
        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;
            }
        }
Ejemplo n.º 3
0
        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;
            }
        }
Ejemplo n.º 4
0
        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;
            }
        }
Ejemplo n.º 5
0
        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;
            }
        }
Ejemplo n.º 6
0
        public HttpResponseMessage GetSingleEvent(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 eventEntity= context.Events.FirstOrDefault(e=>e.Id==eventId);

                if(eventEntity==null)
                {
                    throw new ArgumentException("There is no such event!");
                }
                var singleEvent = new EventModel()
                                {
                                    Id = eventEntity.Id,
                                    Name = eventEntity.Name,
                                    Description = eventEntity.Description,
                                    Country = eventEntity.Country,
                                    Town = eventEntity.Town,
                                    Address = eventEntity.Address,
                                    Creator = eventEntity.Creator.FullName,

                                    Comments = from comment in eventEntity.Comments
                                               select new CommentModel()
                                               {
                                                   Text = comment.Text,
                                                   UserName = comment.User.FullName
                                               },
                                    InvitedUsers = from invitesUsers in eventEntity.UsersEvents.Where(u => u.State == State.NotAnswered)
                                                   select new ProfileUserModel()
                                                   {
                                                       FullName = invitesUsers.User.FullName,
                                                       PictureUrl = invitesUsers.User.PictureUrl
                                                   },
                                    Confirmed = from confirmedUsers in eventEntity.UsersEvents.Where(u => u.State == State.Confirmed)
                                                select new ProfileUserModel()
                                                {
                                                    FullName = confirmedUsers.User.FullName,
                                                    PictureUrl = confirmedUsers.User.PictureUrl
                                                },
                                    Declined = from declinedUsers in eventEntity.UsersEvents.Where(u => u.State == State.Declined)
                                               select new ProfileUserModel()
                                               {
                                                   FullName = declinedUsers.User.FullName,
                                                   PictureUrl = declinedUsers.User.PictureUrl
                                               }
                                };
                var response = this.Request.CreateResponse(HttpStatusCode.OK, singleEvent);
                return response;
            }
            catch (Exception ex)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return response;
            }
        }
Ejemplo n.º 7
0
        public HttpResponseMessage GetAllEvents(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 eventEntities = context.Events.Where(e=>e.UsersEvents
                    .Any(ue=>ue.UserId==user.Id)).OrderByDescending(e=>e.Id);

                var events = from singleEvent in eventEntities
                             select new EventModel()
                             {
                                 Id = singleEvent.Id,
                                 Name = singleEvent.Name,
                                 Creator = singleEvent.Creator.FullName,
                             };
                var response = this.Request.CreateResponse(HttpStatusCode.OK, events);
                return response;
            }
            catch (Exception ex)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return response;
            }
        }
Ejemplo n.º 8
0
        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 GetSingleEvent(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 eventEntity = context.MasterEvents.FirstOrDefault(e => e.Id == eventId);

                if (eventEntity == null)
                {
                    throw new ArgumentException("There is no such event!");
                }

                var singleEvent = new MasterEventModel()
                {
                    Id = eventEntity.Id,
                    Name = eventEntity.Name,
                    Description = eventEntity.Description,
                    Country = eventEntity.Country,
                    Town = eventEntity.Town,
                    Address = eventEntity.Address,
                    Creator = eventEntity.Creator.FullName,
                    Comments = (eventEntity.Comments!=null)?from comment in eventEntity.Comments
                               select new CommentModel()
                               {
                                   Text = comment.Text,
                                   UserName = comment.User.FullName
                               } : new List<CommentModel>(),
                    Users = (eventEntity.Users!=null)?from confirmedUsers in eventEntity.Users
                                select new ProfileUserModel()
                                {
                                    FullName = confirmedUsers.FullName,
                                    PictureUrl = confirmedUsers.PictureUrl
                                }:new LinkedList<ProfileUserModel>()
                    //Comments = from comment in eventEntity.Comments
                    //           select new CommentModel()
                    //              {
                    //                 Text = comment.Text,
                    //                 UserName = comment.User.FullName
                    //              },
                    //Users = from confirmedUser in eventEntity.Users
                    //        select new ProfileUserModel()
                    //                   {
                    //                       FullName = confirmedUser.FullName,
                    //                       PictureUrl = confirmedUser.PictureUrl
                    //                   }

                };
                var response = this.Request.CreateResponse(HttpStatusCode.OK, singleEvent);
                return response;
            }
            catch (Exception ex)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return response;
            }
        }
        public HttpResponseMessage GetAllEventsWithDetails(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 eventEntities = context.MasterEvents;

                var events = from singleEvent in eventEntities
                             select new MasterEventModel()
                             {
                                 Name = singleEvent.Name,
                                 Description = singleEvent.Description,
                                 Country = singleEvent.Country,
                                 Town = singleEvent.Town,
                                 Address = singleEvent.Address,
                                 Creator = singleEvent.Creator.FullName,

                                 Comments = from comment in singleEvent.Comments
                                            select new CommentModel()
                                            {
                                                Text = comment.Text,
                                                UserName = comment.User.FullName
                                            },
                                 Users = from confirmedUsers in singleEvent.Users
                                             select new ProfileUserModel()
                                             {
                                                 FullName = confirmedUsers.FullName,
                                                 PictureUrl = confirmedUsers.PictureUrl
                                             }
                             };
                var response = this.Request.CreateResponse(HttpStatusCode.OK, events);
                return response;
            }
            catch (Exception ex)
            {
                var response = this.Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return response;
            }
        }
Ejemplo n.º 11
0
        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;
            }
        }
Ejemplo n.º 12
0
        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;
            }
        }
Ejemplo n.º 13
0
        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;
            }
        }
Ejemplo n.º 14
0
        public HttpResponseMessage GetAllUsers()
        {
            try
            {
                var context = new TheSocialEventsContext();
                using (context)
                {
                    var users = (from user in context.Users
                                  select new UserModel()
                                  {
                                      Email = user.Email,
                                      FullName = user.FullName,
                                      PictureUrl = user.PictureUrl,
                                  }).ToList();

                    var response = Request.CreateResponse(HttpStatusCode.OK, users);
                    return response;
                }
            }
            catch (Exception ex)
            {
                var response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return response;
            }
        }
Ejemplo n.º 15
0
        public HttpResponseMessage GetAllFriends(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.");
                    }

                    var friends = from friend in user.Friends
                                  select new UserModel()
                                             {
                                                 Email = friend.Email,
                                                 FullName = friend.FullName,
                                                 PictureUrl = friend.PictureUrl,
                                             };

                    var response = Request.CreateResponse(HttpStatusCode.OK, friends);
                    return response;
                }
            }
            catch (Exception ex)
            {
                var response = Request.CreateResponse(HttpStatusCode.BadRequest, ex.Message);
                return response;
            }
        }