Beispiel #1
0
        public IHttpActionResult GetGroupsForUser([FromUri] string username)
        {
            var user = this.Data.Users.FirstOrDefault(u => u.UserName == username);

            if (user == null)
            {
                return(this.NotFound());
            }

            var userId = user.Id;

            var groupsWhereMember = user.Groups
                                    .Select(g => CreateGroupViewModel.CreateGroupPreview(user, g))
                                    .ToList();

            var groupsWhereOwner = this.Data.Groups
                                   .Where(g => g.Owner.Id == userId)
                                   .ToList()
                                   .Select(g => CreateGroupViewModel.CreateGroupPreview(user, g))
                                   .ToList();

            var totalGroups = groupsWhereMember.Union(groupsWhereOwner);

            return(this.Ok(totalGroups));
        }
Beispiel #2
0
        public IHttpActionResult Leave(int groupId)
        {
            var group = this.Data.Groups.Find(groupId);

            var currentUserId = this.User.Identity.GetUserId();
            var currentUser   = this.Data.Users.FirstOrDefault(u => u.Id == currentUserId);

            if (currentUser == null)
            {
                return(this.BadRequest("Invalid user token! Please login again!"));
            }

            if (group == null)
            {
                return(this.NotFound());
            }

            var member = group.Members.FirstOrDefault(m => m.Id == currentUserId);

            if (member == null)
            {
                return(this.BadRequest("You are not a member of this group."));
            }

            group.Members.Remove(member);
            this.Data.SaveChanges();

            return(this.Ok(CreateGroupViewModel.CreateGroupPreview(currentUser, group)));
        }
Beispiel #3
0
        public IHttpActionResult GetGroupById([FromUri] int id)
        {
            var group = this.Data.Groups.Find(id);

            if (group == null)
            {
                return(this.NotFound());
            }

            var currentUserId = this.User.Identity.GetUserId();
            var currentUser   = this.Data.Users.FirstOrDefault(u => u.Id == currentUserId);

            if (currentUser == null)
            {
                return(this.BadRequest("Invalid user token! Please login again!"));
            }

            return(this.Ok(CreateGroupViewModel.CreateGroupPreview(currentUser, group)));
        }
Beispiel #4
0
        public IHttpActionResult SearchGroupByName([FromUri] string searchTerm)
        {
            var currentUserId = User.Identity.GetUserId();
            var currentUser   = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId);

            if (currentUser == null)
            {
                return(this.BadRequest("Invalid user token! Please login again!"));
            }

            string searchName = searchTerm.ToUpper();

            var groupsFound = this.Data.Groups
                              .Where(g => g.Name.ToUpper().Contains(searchName) || g.Description.ToUpper().Contains(searchName))
                              .ToList()
                              .Select(g => CreateGroupViewModel.CreateGroupPreview(currentUser, g));

            return(this.Ok(groupsFound));
        }
Beispiel #5
0
        public IHttpActionResult CreateGroup(CreateGroupBindingModel model)
        {
            if (model == null)
            {
                return(this.BadRequest("Missing data about group."));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            if (this.Data.Groups.Any(g => g.Name == model.Name))
            {
                return(this.BadRequest("The group already exists."));
            }

            var currentUserId = this.User.Identity.GetUserId();
            var currentUser   = this.Data.Users.FirstOrDefault(u => u.Id == currentUserId);

            if (currentUser == null)
            {
                return(this.BadRequest("Invalid user token! Please login again!"));
            }

            var group = new Group()
            {
                Name        = model.Name,
                Description = model.Description,
                CreatedOn   = DateTime.Now,
                WallPicture = model.WallPicture,
                Owner       = currentUser
            };

            this.Data.Groups.Add(group);
            this.Data.SaveChanges();

            var viewModel = CreateGroupViewModel.CreateGroupPreview(currentUser, group);

            return(this.Ok(viewModel));
        }
Beispiel #6
0
        public IHttpActionResult EditGroupById([FromUri] int groupId, [FromBody] EditGroupBindingModel model)
        {
            var group = this.Data.Groups.Find(groupId);

            if (group == null)
            {
                return(this.NotFound());
            }

            if (!ModelState.IsValid)
            {
                return(this.BadRequest(this.ModelState));
            }

            var currentUserId = User.Identity.GetUserId();
            var currentUser   = this.Data.Users.FirstOrDefault(x => x.Id == currentUserId);

            if (currentUser == null)
            {
                return(this.BadRequest("Invalid user token! Please login again!"));
            }

            if (currentUser != group.Owner)
            {
                return(this.BadRequest("Not allowed. User must be owner of group."));
            }

            group.Name        = model.Name;
            group.Description = model.Description;
            group.WallPicture = model.WallPicture;
            this.Data.SaveChanges();

            var dbGroup = this.Data.Groups.Find(group.Id);

            return(this.Ok(CreateGroupViewModel.CreateGroupPreview(currentUser, dbGroup)));
        }