public async Task <IActionResult> RegisterOrganisationAsync( [FromRoute] string organisationId, [FromBody] OrganisationRegistration organisation, [FromServices] IOrganisationService service) { if (string.IsNullOrWhiteSpace(organisationId) || organisation == null) { return(BadRequest(new { reason = $"Invalid request!", request = organisation, organisationId })); } if (!organisationId.Equals(organisation.Id)) { return(BadRequest(new { reason = $"Invalid organisation name!", request = organisation, organisationId })); } try { organisation.CreatedAt = DateTime.Now; var organisationResult = await service.RegisterAsync(organisation); return(CreatedAtRoute(nameof(GetOrganisationAsync), new { organisationId }, organisationResult)); } catch (NameAlreadyUsedException ex) { return(BadRequest(new { reason = ex.Message })); } catch (Exception ex) { Console.WriteLine(ex); return(StatusCode(statusCode: (int)HttpStatusCode.InternalServerError)); } }
public async Task <Organisation> RegisterAsync(OrganisationRegistration organisationContract) { if (await repository.ExistsByAsync(o => o.Id == organisationContract.Id)) { throw new NameAlreadyUsedException(organisationContract.Name); } var organisationEntity = organisationContract.ToEntity(); organisationEntity = await repository.AddAsync(organisationEntity); var user = GeneratePasswordAndAttachAdminRole(organisationContract.AdminUser, organisationContract.Id); user.CreatedAt = DateTime.Now; user = await userRepository.AddAsync(user); var administrationTeam = new Domain.Entities.Team { Name = "org-admin", TeamCode = "org-admin", OrganisationId = organisationContract.Id, CreatedAt = DateTime.Now }; administrationTeam.Users.Add(new Domain.Entities.TeamUser(user)); administrationTeam = await teamRepository.AddAsync(administrationTeam); user.TeamsPermissions.Add(new Domain.Entities.TeamPermission { TeamCode = administrationTeam.TeamCode, IsOwner = true }); user = await userRepository.UpdateAsync(user); return(organisationEntity.ToContract()); }