public ActionResult RemoveWriterFromWorld(WriterWorld removerequest, [FromHeader(Name = "Authorization")] string jwt) { try { return(Ok(_worldUserManagementService.DeleteWriterFromWorld(removerequest, jwt).Result)); } catch (NotAuthorisedException ex) { return(Unauthorized(ex.Message)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
public ActionResult AddWriterToWorld(WriterWorld addRequest, [FromHeader(Name = "Authorization")] string jwt) { try { return(Ok(_worldUserManagementService.AddWriterToWorld(addRequest, jwt).Result)); } catch (UserIsAlreadyAWriterException ex) { return(BadRequest(ex.Message)); } catch (NotAuthorisedException ex) { return(Unauthorized(ex.Message)); } catch (Exception ex) { return(BadRequest(ex.Message)); } }
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."); } }
public async Task <bool> DeleteWriterFromWorld(WriterWorld writerWorld, string jwt) { //step 1: Get world World world = await _worldRepository.Get(writerWorld.WorldId); if (world.Owner.Id == _authenticationHelper.getUserIdFromToken(jwt)) { if (world == null) { throw new WorldNotFoundException("The world with the id: " + writerWorld.WorldId + " Does not exist"); } //step 2: check if writer is indeed a writer on this world User writerInWorld = null; foreach (User writer in world.Writers) { if (writer.Id == writerWorld.WriterId) { writerInWorld = writer; break; } } if (writerInWorld == null) { throw new WriterDoesNotExistInWorldException("Writer does not exist in world."); } //step 3 remove writer world.Writers.Remove(writerInWorld); await _worldRepository.Update(world.Id, world); return(true); } else { throw new NotAuthorisedException("You are not eligble to remove a writer to this world."); } }