/// <summary> /// Create a group by an user /// </summary> /// <param name="groupDto">The group details</param> /// <param name="userId">The user that create the group</param> /// <returns>The current subscripted groups of the user</returns> public IEnumerable <Group> Create(GroupIncomingDTO groupDto, int userId) { if (!UserExist(userId)) { _logger.LogError($"Couldn't create group for non-existing user {userId}"); throw new NullReferenceException("Couldn't create group for non-existing user"); } Group group = _mapper.Map <Group>(groupDto); Subscription subscription = new Subscription(); subscription.UserID = userId; subscription.Group = group; subscription.DateOfSubscription = DateTime.UtcNow; try { _unitOfWork.GroupRepository.Create(group); _unitOfWork.SubscriptionRepository.Create(subscription); _unitOfWork.SaveChanges(); } catch (Exception exception) { _logger.LogError($"Could not create group: {groupDto.Name} by user:{userId} error: {exception.Message}"); throw exception; } IEnumerable <Group> groupsOfUser = _unitOfWork.GroupRepository.FindAllGroupOfUser(userId); _logger.LogInformation($"Created group: {groupDto.Name} by user: {userId}"); return(groupsOfUser); }
//Mock group response //Without including subscribtion data private IEnumerable <Group> mockGroupResponse(GroupIncomingDTO groupDto) { Group group = _mapper.Map <Group>(groupDto); List <Group> result = new List <Group>(); result.Add(group); return(result); }
public async System.Threading.Tasks.Task Test_CreateGroup_NonExistingUser() { GroupService groupService = new GroupService(_mapper, _unitOfWorkMock.Object, _loggerFactory.CreateLogger <IGroupService>(), _emailHandler.Object); GroupIncomingDTO newGroup = new GroupIncomingDTO() { ColorID = 1, Description = "Test description", IconID = 1, Name = "House" }; _groupRepositoryMock.Setup(u => u.FindAllGroupOfUser(1)).Returns(mockGroupResponse(newGroup)); Assert.Throws <InvalidInputException>(() => groupService.Create(newGroup, 2)); }
public async System.Threading.Tasks.Task Test_UpdateGroup() { GroupService groupService = new GroupService(_mapper, _unitOfWorkMock.Object, _loggerFactory.CreateLogger <IGroupService>(), _emailHandler.Object); Color color = new Color() { ID = 1, Name = "Pink", Value = "#5c6bc0" }; Icon icon = new Icon() { ID = 1, Name = "Natuur", Value = "nature_people" }; Group existingGroup = new Group() { ID = 1, Color = color, Description = "Test description", Icon = icon, Name = "House", Members = new List <Subscription>() }; GroupIncomingDTO updateGroup = new GroupIncomingDTO() { ColorID = 1, Description = "hallo", IconID = 1, Name = "Test" }; _colorRepositoryMock.Setup(u => u.GetColorByValue(updateGroup.ColorID)).Returns(color); _iconRepositoryMock.Setup(u => u.GetIconByValue(updateGroup.IconID)).Returns(icon); _groupRepositoryMock.Setup(u => u.FindGroupOfUser(1, 1)).Returns(existingGroup); Group result = groupService.Update(existingGroup.ID, updateGroup, _mockID); Assert.NotNull(result); Assert.Equal(updateGroup.Name, result.Name); Assert.Equal(updateGroup.Description, result.Description); }
public async System.Threading.Tasks.Task Test_CreateGroup() { GroupService groupService = new GroupService(_mapper, _unitOfWorkMock.Object, _loggerFactory.CreateLogger <IGroupService>(), _emailHandler.Object); GroupIncomingDTO newGroup = new GroupIncomingDTO() { ColorID = 1, Description = "Test description", IconID = 1, Name = "House" }; _groupRepositoryMock.Setup(u => u.FindAllGroupOfUser(1)).Returns(mockGroupResponse(newGroup)); IEnumerable <Group> result = groupService.Create(newGroup, this._mockID); Assert.NotNull(result); Assert.NotEmpty(result); }
/// <summary> /// Update the selected groupd /// </summary> /// <param name="groupId">The group to be updated</param> /// <param name="newGroupData">the updated group data</param> /// <param name="userId">the user that requested the update</param> /// <returns>The updated group</returns> public Group Update(int groupId, GroupIncomingDTO newgroupData, int userId) { Group group; try { group = RetrieveGroupById(groupId, userId); }catch (Exception exception) { _logger.LogError($"Could not update group", exception); throw exception; } Color color = _unitOfWork.ColorRepository.GetColorByValue(newgroupData.ColorID); group.Color = color; Icon icon = _unitOfWork.IconRepository.GetIconByValue(newgroupData.IconID); group.Icon = icon; if (!string.IsNullOrEmpty(newgroupData.Description)) { group.Description = newgroupData.Description; } if (!string.IsNullOrEmpty(newgroupData.Name)) { group.Name = newgroupData.Name; } try { _unitOfWork.GroupRepository.Update(group); _unitOfWork.SaveChanges(); }catch (Exception exception) { _logger.LogError($"Could not update group", exception); throw exception; } _logger.LogInformation($"Updated group: {group} by user: {userId}"); return(group); }
public async Task <ActionResult <GroupOutgoingDTO> > Update(int id, [FromBody] GroupIncomingDTO groupDto) { int userId = HttpContext.User.GetCurrentUserId(); try { Group group = _groupService.Update(id, groupDto, userId); GroupOutgoingDTO response = _mapper.Map <GroupOutgoingDTO>(group); return(Ok(response)); } catch (InvalidInputException invalidInputException) { _logger.LogInformation($"User {userId} could not update groupd with id: {id}", invalidInputException); return(BadRequest(_localizer["DeleteGroup_Error"].Value)); } catch (Exception exception) { _logger.LogError($"User {userId} could not update groupd with id: {id}", exception); return(StatusCode(500, _localizer["InternalError"].Value)); } }
public async Task <ActionResult <IEnumerable <GroupOutgoingDTO> > > Create([FromBody] GroupIncomingDTO groupDto) { int userId = HttpContext.User.GetCurrentUserId(); try { IEnumerable <Group> groups = _groupService.Create(groupDto, userId); IEnumerable <GroupOutgoingDTO> groupDtos = _mapper.Map <IEnumerable <GroupOutgoingDTO> >(groups); return(Ok(groupDtos)); } catch (InvalidInputException invalidInputException) { _logger.LogInformation($"User {userId} could not create group", groupDto, invalidInputException); return(BadRequest(_localizer["CreateGroup_Error"].Value)); } catch (Exception exception) { _logger.LogError($"User {userId} could not create group", groupDto, exception); return(StatusCode(500, _localizer["InternalError"].Value)); } }