public async Task <ActionResult <GroupDto> > CreateGroup([FromBody] GroupCreationDto group)
        {
            try
            {
                await _azureAdRepository.GetGroup(group.Id.ToString());
            }
            catch (ServiceException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    ModelState.AddModelError("azureAdGroupNotFound",
                                             $"Group with id: {group.Id} was not found on Azure AD");
                }
            }

            var groupExists = await _groupRepository.GroupExists(group.Id);

            if (groupExists)
            {
                return(Conflict("Group already exists"));
            }

            if (group.SmartLockGroups.Count > 0)
            {
                foreach (var smartLockGroup in group.SmartLockGroups)
                {
                    var smartLockExist = await _smartLockRepository.SmartLockExists(smartLockGroup.SmartLockId);

                    if (!smartLockExist)
                    {
                        ModelState.AddModelError("smartLockNotExist",
                                                 $"Smart lock with id: {smartLockGroup.SmartLockId} doesn't exist");
                    }
                }
            }

            if (!ModelState.IsValid)
            {
                return(ValidationProblem(ModelState));
            }

            var groupEntity = _mapper.Map <Group>(group);

            _groupRepository.AddGroup(groupEntity);
            await _groupRepository.Save();

            var groupDto = _mapper.Map <GroupDto>(groupEntity);

            return(CreatedAtAction("GetGroup", new { groupId = groupDto.Id }, groupDto));
        }
Exemple #2
0
        public async Task <ActionResult <IEnumerable <GroupDto> > > GetGroups(Guid userId)
        {
            var userExists = await _userRepository.UserExists(userId);

            if (!userExists)
            {
                return(NotFound());
            }

            var allGroupsFromRepo = await _groupRepository.GetGroups();

            var client = await MicrosoftGraphClient.GetGraphServiceClient();

            var userGroupsIdsFromAzureAd = await _azureAdRepository
                                           .GetUserGroupsIds(client, userId.ToString());

            var userGroupsFromAzureAd = new List <Group>();

            foreach (var groupId in userGroupsIdsFromAzureAd)
            {
                var group = await _azureAdRepository
                            .GetGroup(groupId.ToString());

                userGroupsFromAzureAd.Add(group);
            }

            var mergedGroups = DataMerger.MergeGroupsWithAzureData(allGroupsFromRepo,
                                                                   userGroupsFromAzureAd, _mapper);

            return(Ok(mergedGroups));
        }
        public async Task <ActionResult <SmartLockGroupDto> > AddSmartLockGroup(Guid smartLockId,
                                                                                SmartLockGroupCreationDto smartLockGroup)
        {
            try
            {
                await _azureAdRepository.GetGroup(smartLockGroup.GroupId.ToString());
            }
            catch (ServiceException e)
            {
                if (e.StatusCode == HttpStatusCode.NotFound)
                {
                    _logger.LogWarning("Group  was not found on Azure AD");
                    ModelState.AddModelError("azureAdGroupNotFound",
                                             $"Group with id: {smartLockGroup.GroupId} was not found on Azure AD");
                }
            }

            if (!ModelState.IsValid)
            {
                return(ValidationProblem(ModelState));
            }

            if (await _smartLockRepository.SmartLockGroupExists(smartLockId, smartLockGroup.GroupId))
            {
                _logger.LogWarning("Group already exists for this smart lock");
                return(Conflict("Group already exists for this smart lock"));
            }

            _smartLockRepository.AddSmartLockGroup(smartLockId, smartLockGroup.GroupId);
            await _smartLockRepository.Save();

            return(CreatedAtAction("GetSmartLockGroup", new { smartLockId = smartLockId, groupId = smartLockGroup.GroupId },
                                   smartLockGroup));
        }
        public async Task <ActionResult <AzureAdGroupDto> > GetGroup(string groupId)
        {
            try
            {
                // Load group profile.
                var group = await _azureAdRepository.GetGroup(groupId);

                var groupDto = _mapper.Map <AzureAdGroupDto>(group);

                var groupExist = await _groupRepository.GroupExists(groupDto.Id);

                if (groupExist)
                {
                    groupDto.AddedToDb = true;
                }

                return(Ok(groupDto));
            }
            catch (ServiceException ex)
            {
                if (ex.StatusCode == HttpStatusCode.BadRequest)
                {
                    return(BadRequest());
                }
                else
                {
                    return(NotFound());
                }
            }
        }