func (c *SpaceController) Create(ctx *app.CreateSpaceContext) error {
	currentUser, err := login.ContextIdentity(ctx)
	if err != nil {
		return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
	}

	err = validateCreateSpace(ctx)
	if err != nil {
		return jsonapi.JSONErrorResponse(ctx, err)
	}

	reqSpace := ctx.Payload.Data
	spaceName := *reqSpace.Attributes.Name
	spaceID := uuid.NewV4()
	// Create keycloak resource for this space
	// TODO if transaction below fails we need to remove this Keycloak Resource to avoid poluting Keycloak with unused resources
	resource, err := c.resourceManager.CreateResource(ctx, ctx.RequestData, spaceID.String(), spaceResourceType, &spaceName, &scopes, currentUser.String(), spaceName+"-"+uuid.NewV4().String())
	if err != nil {
		return jsonapi.JSONErrorResponse(ctx, err)
	}

	spaceResource := &space.Resource{
		ResourceID:   resource.ResourceID,
		PolicyID:     resource.PolicyID,
		PermissionID: resource.PermissionID,
		SpaceID:      spaceID,
	}

	return application.Transactional(c.db, func(appl application.Application) error {
		newSpace := space.Space{
			ID:      spaceID,
			Name:    spaceName,
			OwnerId: *currentUser,
		}
		if reqSpace.Attributes.Description != nil {
			newSpace.Description = *reqSpace.Attributes.Description
		}

		rSpace, err := appl.Spaces().Create(ctx, &newSpace)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}
		/*
			Should we create the new area
			- over the wire(service) something like app.NewCreateSpaceAreasContext(..), OR
			- as part of a db transaction ?

			The argument 'for' creating it at a transaction level is :
			You absolutely need both space creation + area creation
			to happen in a single transaction as per requirements.
		*/

		newArea := area.Area{
			ID:      uuid.NewV4(),
			SpaceID: rSpace.ID,
			Name:    rSpace.Name,
		}
		err = appl.Areas().Create(ctx, &newArea)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, errs.Wrapf(err, "failed to create area: %s", rSpace.Name))
		}

		spaceData, err := ConvertSpaceFromModel(ctx.Context, c.db, ctx.RequestData, *rSpace)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}
		res := &app.SpaceSingle{
			Data: spaceData,
		}

		// Create space resource which will represent the keyclok resource associated with this space
		_, err = appl.SpaceResources().Create(ctx, spaceResource)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}

		ctx.ResponseData.Header().Set("Location", rest.AbsoluteURL(ctx.RequestData, app.SpaceHref(res.Data.ID)))
		return ctx.Created(res)
	})
}
// Create runs the create action.
Create runs the create action.
// Create runs the create action .
func (c *SpaceController) Create(ctx *app.CreateSpaceContext) error {
	currentUser, err := login.ContextIdentity(ctx)
	if err != nil {
		return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
	}
	err = validateCreateSpace(ctx)
	if err != nil {
		return jsonapi.JSONErrorResponse(ctx, err)
	}
	reqSpace := ctx.Payload.Data
	spaceName := *reqSpace.Attributes.Name
	spaceID := uuid.NewV4()
	resource, err := c.resourceManager.CreateResource(ctx, ctx.RequestData, spaceID.String(), spaceResourceType, &spaceName, &scopes, currentUser.String(), spaceName+"-"+uuid.NewV4().String())
	if err != nil {
		return jsonapi.JSONErrorResponse(ctx, err)
	}
	spaceResource := &space.Resource{
		ResourceID:   resource.ResourceID,
		PolicyID:     resource.PolicyID,
		PermissionID: resource.PermissionID,
		SpaceID:      spaceID,
	}
	return application.Transactional(c.db, func(appl application.Application) error {
		newSpace := space.Space{
			ID:      spaceID,
			Name:    spaceName,
			OwnerId: *currentUser,
		}
		if reqSpace.Attributes.Description != nil {
			newSpace.Description = *reqSpace.Attributes.Description
		}
		rSpace, err := appl.Spaces().Create(ctx, &newSpace)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}
		newArea := area.Area{
			ID:      uuid.NewV4(),
			SpaceID: rSpace.ID,
			Name:    rSpace.Name,
		}
		err = appl.Areas().Create(ctx, &newArea)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, errs.Wrapf(err, "failed to create area: %s", rSpace.Name))
		}
		spaceData, err := ConvertSpaceFromModel(ctx.Context, c.db, ctx.RequestData, *rSpace)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}
		res := &app.SpaceSingle{
			Data: spaceData,
		}
		_, err = appl.SpaceResources().Create(ctx, spaceResource)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}
		ctx.ResponseData.Header().Set("Location", rest.AbsoluteURL(ctx.RequestData, app.SpaceHref(res.Data.ID)))
		return ctx.Created(res)
	})
}
func ( c * SpaceController ) Create ( ctx * app . CreateSpaceContext ) error { currentUser , err := login . ContextIdentity ( ctx ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , goa . ErrUnauthorized ( err . Error ( ) ) ) } err = validateCreateSpace ( ctx ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , err ) } reqSpace := ctx . Payload . Data spaceName := * reqSpace . Attributes . Name spaceID := uuid . NewV4 ( ) resource , err := c . resourceManager . CreateResource ( ctx , ctx . RequestData , spaceID . String ( ) , spaceResourceType , & spaceName , & scopes , currentUser . String ( ) , spaceName + " " + uuid . NewV4 ( ) . String ( ) ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , err ) } spaceResource := & space . Resource { ResourceID : resource . ResourceID , PolicyID : resource . PolicyID , PermissionID : resource . PermissionID , SpaceID : spaceID , } return application . Transactional ( c . db , func ( appl application . Application ) error { newSpace := space . Space { ID : spaceID , Name : spaceName , OwnerId : * currentUser , } if reqSpace . Attributes . Description != nil { newSpace . Description = * reqSpace . Attributes . Description } rSpace , err := appl . Spaces ( ) . Create ( ctx , & newSpace ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , err ) } newArea := area . Area { ID : uuid . NewV4 ( ) , SpaceID : rSpace . ID , Name : rSpace . Name , } err = appl . Areas ( ) . Create ( ctx , & newArea ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , errs . Wrapf ( err , " " , rSpace . Name ) ) } spaceData , err := ConvertSpaceFromModel ( ctx . Context , c . db , ctx . RequestData , * rSpace ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , err ) } res := & app . SpaceSingle { Data : spaceData , } _ , err = appl . SpaceResources ( ) . Create ( ctx , spaceResource ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , err ) } ctx . ResponseData . Header ( ) . Set ( " " , rest . AbsoluteURL ( ctx . RequestData , app . SpaceHref ( res . Data . ID ) ) ) return ctx . Created ( res ) } ) }
Create runs the create action.
Create runs the create action .
// Create keycloak resource for this space // TODO if transaction below fails we need to remove this Keycloak Resource to avoid poluting Keycloak with unused resources /*
			Should we create the new area
			- over the wire(service) something like app.NewCreateSpaceAreasContext(..), OR
			- as part of a db transaction ?

			The argument 'for' creating it at a transaction level is :
			You absolutely need both space creation + area creation
			to happen in a single transaction as per requirements.
		*/ // Create space resource which will represent the keyclok resource associated with this space