Exemple #1
0
        public IActionResult Post([FromBody] GroupMemberViewModel groupMemberViewModel)
        {
            try
            {
                var group = _repository.GetGroupById(groupMemberViewModel.GroupId);
                if (group == null)
                {
                    return(BadRequest($"Group does not exist. [id={groupMemberViewModel.GroupId}]"));
                }

                var user = _repository.GetUser(groupMemberViewModel.Member.UserName);
                if (user == null)
                {
                    return(BadRequest($"User not found. [user={groupMemberViewModel.Member.UserName}]"));
                }

                foreach (var member in group.Members)
                {
                    if (member.MemberId.Equals(user.Id))
                    {
                        return(BadRequest($"User already in group. [user={user.Id}]"));
                    }
                }

                var groupMember = _mapper.Map <GroupMemberViewModel, GroupMember>(groupMemberViewModel);

                group.Members.Add(groupMember);
                user.Memberships.Add(groupMember);

                if (_repository.SaveAll())
                {
                    return(Ok());
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save group membership changes. [ex={ex}]");

                throw;
            }

            return(BadRequest("Failed to save group membership changes."));
        }
        public IActionResult Post([FromBody] GroupViewModel group)
        {
            try
            {
                if (ModelState.IsValid)
                {
                    var newGroup = _mapper.Map <GroupViewModel, Group>(group);

                    if (newGroup.CreationTime == DateTime.MinValue)
                    {
                        newGroup.CreationTime = DateTime.UtcNow;
                    }

                    var user  = _repository.GetUser(User.Identity.Name);
                    var admin = new GroupMember()
                    {
                        Group    = newGroup,
                        GroupId  = newGroup.Id,
                        Member   = user,
                        MemberId = user.Id,
                        IsAdmin  = true
                    };
                    newGroup.Members = new List <GroupMember>()
                    {
                        admin
                    };
                    user.Memberships.Add(admin);

                    _repository.AddEntity(newGroup);
                    if (_repository.SaveAll())
                    {
                        var vm = _mapper.Map <Group, GroupViewModel>(newGroup);

                        return(Created($"api/groups/{vm.GroupId}", vm));
                    }
                }
                else
                {
                    return(BadRequest(ModelState));
                }
            }
            catch (Exception ex)
            {
                _logger.LogError($"Failed to save group. [ex={ex}]");

                throw;
            }

            return(BadRequest("Failed to save new group."));
        }