func (c *UsersController) Update(ctx *app.UpdateUsersContext) error {

	id, err := login.ContextIdentity(ctx)
	if err != nil {
		return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
	}

	return application.Transactional(c.db, func(appl application.Application) error {
		identity, err := appl.Identities().Load(ctx, *id)
		if err != nil || identity == nil {
			log.Error(ctx, map[string]interface{}{
				"identity_id": id,
			}, "auth token contains id %s of unknown Identity", *id)
			jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrUnauthorized(fmt.Sprintf("Auth token contains id %s of unknown Identity\n", *id)))
			return ctx.Unauthorized(jerrors)
		}

		var user *account.User
		if identity.UserID.Valid {
			user, err = appl.Users().Load(ctx.Context, identity.UserID.UUID)
			if err != nil {
				return jsonapi.JSONErrorResponse(ctx, errors.Wrap(err, fmt.Sprintf("Can't load user with id %s", identity.UserID.UUID)))
			}
		}

		updatedEmail := ctx.Payload.Data.Attributes.Email
		if updatedEmail != nil {
			user.Email = *updatedEmail
		}
		updatedBio := ctx.Payload.Data.Attributes.Bio
		if updatedBio != nil {
			user.Bio = *updatedBio
		}
		updatedFullName := ctx.Payload.Data.Attributes.FullName
		if updatedFullName != nil {
			user.FullName = *updatedFullName
		}
		updatedImageURL := ctx.Payload.Data.Attributes.ImageURL
		if updatedImageURL != nil {
			user.ImageURL = *updatedImageURL
		}
		updateURL := ctx.Payload.Data.Attributes.URL
		if updateURL != nil {
			user.URL = *updateURL
		}

		updatedContextInformation := ctx.Payload.Data.Attributes.ContextInformation
		if updatedContextInformation != nil {
			// if user.ContextInformation , we get to PATCH the ContextInformation field,
			// instead of over-writing it altogether. Note: The PATCH-ing is only for the
			// 1st level of JSON.
			if user.ContextInformation == nil {
				user.ContextInformation = workitem.Fields{}
			}
			for fieldName, fieldValue := range updatedContextInformation {
				// Save it as is, for short-term.
				user.ContextInformation[fieldName] = fieldValue
			}
		}

		err = appl.Users().Save(ctx, user)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}

		err = appl.Identities().Save(ctx, identity)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}

		return ctx.OK(ConvertUser(ctx.RequestData, identity, user))
	})
}
// Update updates the authorized user based on the provided Token
Update updates the authorized user based on the provided Token
// Update updates the authorized user based on the provided Token
func (c *UsersController) Update(ctx *app.UpdateUsersContext) error {
	id, err := login.ContextIdentity(ctx)
	if err != nil {
		return jsonapi.JSONErrorResponse(ctx, goa.ErrUnauthorized(err.Error()))
	}
	return application.Transactional(c.db, func(appl application.Application) error {
		identity, err := appl.Identities().Load(ctx, *id)
		if err != nil || identity == nil {
			log.Error(ctx, map[string]interface{}{
				"identity_id": id,
			}, "auth token contains id %s of unknown Identity", *id)
			jerrors, _ := jsonapi.ErrorToJSONAPIErrors(goa.ErrUnauthorized(fmt.Sprintf("Auth token contains id %s of unknown Identity\n", *id)))
			return ctx.Unauthorized(jerrors)
		}
		var user *account.User
		if identity.UserID.Valid {
			user, err = appl.Users().Load(ctx.Context, identity.UserID.UUID)
			if err != nil {
				return jsonapi.JSONErrorResponse(ctx, errors.Wrap(err, fmt.Sprintf("Can't load user with id %s", identity.UserID.UUID)))
			}
		}
		updatedEmail := ctx.Payload.Data.Attributes.Email
		if updatedEmail != nil {
			user.Email = *updatedEmail
		}
		updatedBio := ctx.Payload.Data.Attributes.Bio
		if updatedBio != nil {
			user.Bio = *updatedBio
		}
		updatedFullName := ctx.Payload.Data.Attributes.FullName
		if updatedFullName != nil {
			user.FullName = *updatedFullName
		}
		updatedImageURL := ctx.Payload.Data.Attributes.ImageURL
		if updatedImageURL != nil {
			user.ImageURL = *updatedImageURL
		}
		updateURL := ctx.Payload.Data.Attributes.URL
		if updateURL != nil {
			user.URL = *updateURL
		}
		updatedContextInformation := ctx.Payload.Data.Attributes.ContextInformation
		if updatedContextInformation != nil {
			if user.ContextInformation == nil {
				user.ContextInformation = workitem.Fields{}
			}
			for fieldName, fieldValue := range updatedContextInformation {
				user.ContextInformation[fieldName] = fieldValue
			}
		}
		err = appl.Users().Save(ctx, user)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}
		err = appl.Identities().Save(ctx, identity)
		if err != nil {
			return jsonapi.JSONErrorResponse(ctx, err)
		}
		return ctx.OK(ConvertUser(ctx.RequestData, identity, user))
	})
}
func ( c * UsersController ) Update ( ctx * app . UpdateUsersContext ) error { id , err := login . ContextIdentity ( ctx ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , goa . ErrUnauthorized ( err . Error ( ) ) ) } return application . Transactional ( c . db , func ( appl application . Application ) error { identity , err := appl . Identities ( ) . Load ( ctx , * id ) if err != nil || identity == nil { log . Error ( ctx , map [ string ] interface { } { " " : id , } , " " , * id ) jerrors , _ := jsonapi . ErrorToJSONAPIErrors ( goa . ErrUnauthorized ( fmt . Sprintf ( " \n " , * id ) ) ) return ctx . Unauthorized ( jerrors ) } var user * account . User if identity . UserID . Valid { user , err = appl . Users ( ) . Load ( ctx . Context , identity . UserID . UUID ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , errors . Wrap ( err , fmt . Sprintf ( " " , identity . UserID . UUID ) ) ) } } updatedEmail := ctx . Payload . Data . Attributes . Email if updatedEmail != nil { user . Email = * updatedEmail } updatedBio := ctx . Payload . Data . Attributes . Bio if updatedBio != nil { user . Bio = * updatedBio } updatedFullName := ctx . Payload . Data . Attributes . FullName if updatedFullName != nil { user . FullName = * updatedFullName } updatedImageURL := ctx . Payload . Data . Attributes . ImageURL if updatedImageURL != nil { user . ImageURL = * updatedImageURL } updateURL := ctx . Payload . Data . Attributes . URL if updateURL != nil { user . URL = * updateURL } updatedContextInformation := ctx . Payload . Data . Attributes . ContextInformation if updatedContextInformation != nil { if user . ContextInformation == nil { user . ContextInformation = workitem . Fields { } } for fieldName , fieldValue := range updatedContextInformation { user . ContextInformation [ fieldName ] = fieldValue } } err = appl . Users ( ) . Save ( ctx , user ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , err ) } err = appl . Identities ( ) . Save ( ctx , identity ) if err != nil { return jsonapi . JSONErrorResponse ( ctx , err ) } return ctx . OK ( ConvertUser ( ctx . RequestData , identity , user ) ) } ) }
Update updates the authorized user based on the provided Token
Update updates the authorized user based on the provided Token
// if user.ContextInformation , we get to PATCH the ContextInformation field, // instead of over-writing it altogether. Note: The PATCH-ing is only for the // 1st level of JSON. // Save it as is, for short-term.