public async Task <bool> FollowWorld(Guid userId, Guid worldId, string jwt) { var user = await getUser(userId); var world = await GetWorld(worldId); if (user.Id == _authenticationHelper.getUserIdFromToken(jwt)) { if (user.WorldFollowed == null) { user.WorldFollowed = new List <Guid>(); } if (user.WorldFollowed.Contains(worldId)) { throw new WorldAlreadyFollowedException("The user: "******" already follows the world: " + world.Title + "."); } else { user.WorldFollowed.Add(world.Id); await _userRepository.Update(userId, user); world.Followers.Add(user); await _worldRepository.Update(world.Id, world); return(true); } } else { throw new NotAuthorisedException("You are not authorised to add this user as follower"); } }
public ActionResult <WorldOverviewModel> Post(WorldRequest request, [FromHeader(Name = "Authorization")] string jwt) { var idclaim = _authenticationHelper.getUserIdFromToken(jwt); if (idclaim == request.UserId) { try { var world = _worldManagementService.CreateWorld(request).Result; return(Ok(world)); } catch (Exception ex) { return(BadRequest(ex)); } } else { return(Unauthorized("You are not authorised to do this")); } }
public async Task <bool> AddWriterToWorld(WriterWorld writerWorld, string jwt) { //Step 1: get user Entity from writer id var user = await GetUser(writerWorld.WriterId); if (user == null) { throw new UserNotFoundException("This user does not exist, if this user has just made his account you need to wait a few minutes before trying again"); } //Step 2: get world Entity from world id World world = await _worldRepository.Get(writerWorld.WorldId); if (world.Owner.Id == _authenticationHelper.getUserIdFromToken(jwt))//authorise { if (world == null) { throw new WorldNotFoundException("The world with the id: " + writerWorld.WorldId + " Does not exist"); } //step 3: If world has user already as a writer throw exception foreach (User writer in world.Writers) { if (writer.Id == user.Id) { throw new UserIsAlreadyAWriterException("The user: "******" Already is a writer in this world"); } } //Step 3: update world world.AddWriter(user); await _worldRepository.Update(world.Id, world); return(true); } else { throw new NotAuthorisedException("You are not eligble to remove a writer to this world."); } }