from spire.mesh import ModelController
from spire.schema import SchemaDependency

from tap.resources import UserGroup
from tap.models import UserGroup as UserGroupModel


class UserGroupController(ModelController):
    """User-group controller"""
    mapping = 'id user_id group_id created'
    model = UserGroupModel
    resource = UserGroup
    schema = SchemaDependency('tap')
    version = (1, 0)

    def create(self, request, response, subject, data):
        session = self.schema.session
        instance = self.model(**data)
        session.add(instance)
        session.commit()

        response({'id': self._get_id_value(instance)})
