public async Task <IActionResult> CreateMission(int userId, MissionForCreateDto missionForCreateDto)
        {
            if (userId != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var missionStatus = await _repo.GetMissionStatusByStatusCode(defaultMissionStatus);

            missionForCreateDto.MissionStatusId = missionStatus.Id;

            var mission = new Mission {
                UserId             = userId,
                MissionName        = missionForCreateDto.MissionName,
                IndustryId         = missionForCreateDto.IndustryId,
                BusinessFunctionId = missionForCreateDto.BusinessFunctionId,
                MissionStatusId    = missionForCreateDto.MissionStatusId,
                DesiredOutcome     = missionForCreateDto.DesiredOutcome,
                EstimatedRoi       = missionForCreateDto.EstimatedRoi,
                ActualRoi          = missionForCreateDto.ActualRoi,
                ActualCost         = missionForCreateDto.ActualCost,
                Challenge          = missionForCreateDto.Challenge,
                BusinessImpact     = missionForCreateDto.BusinessImpact,
                TimeFrame          = missionForCreateDto.TimeFrame,
                Public             = missionForCreateDto.Public
            };

            _repo.Add(mission);

            if (await _repo.SaveAll())
            {
                foreach (MissionAccelerator ma in missionForCreateDto.MissionAccelerators)
                {
                    var missionAccelerator = new MissionAccelerator();
                    missionAccelerator.AcceleratorId = ma.AcceleratorId;
                    missionAccelerator.MissionId     = mission.Id;
                    _repo.Add(missionAccelerator);

                    await _repo.SaveAll();
                }

                foreach (MissionPlatform mp in missionForCreateDto.MissionPlatforms)
                {
                    var missionPlatform = new MissionPlatform();
                    missionPlatform.PlatformId = mp.PlatformId;
                    missionPlatform.MissionId  = mission.Id;
                    _repo.Add(missionPlatform);

                    await _repo.SaveAll();
                }
                return(CreatedAtRoute("GetMission", new { userId = userId, missionId = mission.Id }, mission));
            }
            throw new Exception("Creating the mission failed on save");
        }
        public async Task <IActionResult> CreateMessage(int userId, MessageForCreationDto messageForCreationDto)
        {
            var sender = await _repo.GetUser(userId, false);

            if (sender.Id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            messageForCreationDto.SenderId = userId;

            var recipient = await _repo.GetUser(messageForCreationDto.RecipientId, false);

            if (recipient == null)
            {
                return(BadRequest("Could not find user"));
            }

            var message = _mapper.Map <Message>(messageForCreationDto);

            _repo.Add(message);

            if (await _repo.SaveAll())
            {
                var messageToReturn = _mapper.Map <MessageToReturnDto>(message);
                return(CreatedAtRoute("GetMessage", new { id = message.Id }, messageToReturn));
            }

            throw new Exception("Creating the message failed on save");
        }
예제 #3
0
        public async Task <IActionResult> LikeUser(int id, int recipientId)
        {
            if (id != int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value))
            {
                return(Unauthorized());
            }

            var like = await _repo.GetLike(id, recipientId);

            if (like != null)
            {
                return(BadRequest("You already like this user"));
            }

            if (await _repo.GetUser(recipientId, false) == null)
            {
                return(NotFound());
            }

            like = new Like
            {
                LikerId = id,
                LikeeId = recipientId
            };

            _repo.Add <Like>(like);

            if (await _repo.SaveAll())
            {
                return(Ok());
            }

            return(BadRequest("Failed to like user"));
        }
        public async Task <IActionResult> EditMissionTeamMembers(int missionId,
                                                                 MissionTeamMemberEditDto missionTeamMemberEditDto)
        {
            var missionTeam = await _missionControlRepo.GetMissionTeam(missionId);

            var activeMissionTeamMembers   = missionTeam.Where(u => u.Active == true).Select(i => i.UserId);
            var selectedMissionTeamMembers = missionTeamMemberEditDto.MissionTeamMemberUserIds;

            // this result is returning team members that are no longer team members so foreach we need to deactivate
            var activeMissionTeamMembersResult = activeMissionTeamMembers.Except(selectedMissionTeamMembers);

            foreach (var teamMemberUserId in activeMissionTeamMembersResult.Distinct())
            {
                var missionTeamMemberToDeactivate = await _missionControlRepo.GetMissionTeamMember(missionId, teamMemberUserId);

                missionTeamMemberToDeactivate.Active = false;

                if (await _missionControlRepo.SaveAll() == false)
                {
                    return(BadRequest("Failed to deactivate Mission Team Member"));
                }
            }

            selectedMissionTeamMembers = selectedMissionTeamMembers ?? new int[] { };
            // this result is returning new team member so foreach we need to add new
            var selectedMissionTeamMemberResult = selectedMissionTeamMembers.Except(activeMissionTeamMembers);

            foreach (var teamMemberUserId in selectedMissionTeamMemberResult.Distinct())
            {
                var teamMember = await _missionControlRepo.GetMissionTeamMember(missionId, teamMemberUserId);

                if (teamMember != null)
                {
                    teamMember.Active = true;
                }
                else
                {
                    var missionTeamMemberToAdd = new MissionTeam()
                    {
                        UserId    = teamMemberUserId,
                        MissionId = missionId,
                        Active    = true
                    };
                    _missionControlRepo.Add(missionTeamMemberToAdd);
                }

                if (await _missionControlRepo.SaveAll() == false)
                {
                    return(BadRequest("Failed to add Mission Team Member"));
                }
            }

            var missionTeamFromRepo = await _missionControlRepo.GetMissionTeam(missionId);

            var missionTeamToReturn = _mapper.Map <IEnumerable <MissionTeamToReturnDto> >(missionTeamFromRepo);

            return(Ok(missionTeamToReturn));
        }