// Code generated by ent, DO NOT EDIT.

package ogent

import (
	"context"
	"net/http"

	"ariga.io/ogent/internal/integration/ogent/ent"
	"ariga.io/ogent/internal/integration/ogent/ent/alltypes"
	"ariga.io/ogent/internal/integration/ogent/ent/category"
	"ariga.io/ogent/internal/integration/ogent/ent/hat"
	"ariga.io/ogent/internal/integration/ogent/ent/pet"
	"ariga.io/ogent/internal/integration/ogent/ent/schema"
	"ariga.io/ogent/internal/integration/ogent/ent/user"
	"github.com/go-faster/jx"
)

// OgentHandler implements the ogen generated Handler interface and uses Ent as data layer.
type OgentHandler struct {
	client *ent.Client
}

// NewOgentHandler returns a new OgentHandler.
func NewOgentHandler(c *ent.Client) *OgentHandler { return &OgentHandler{c} }

// rawError renders err as json string.
func rawError(err error) jx.Raw {
	var e jx.Encoder
	e.Str(err.Error())
	return e.Bytes()
}

// CreateAllTypes handles POST /all-types-slice requests.
func (h *OgentHandler) CreateAllTypes(ctx context.Context, req *CreateAllTypesReq) (CreateAllTypesRes, error) {
	b := h.client.AllTypes.Create()
	// Add all fields.
	b.SetInt(req.Int)
	b.SetInt8(int8(req.Int8))
	b.SetInt16(int16(req.Int16))
	b.SetInt32(req.Int32)
	b.SetInt64(req.Int64)
	b.SetUint(uint(req.Uint))
	b.SetUint8(uint8(req.Uint8))
	b.SetUint16(uint16(req.Uint16))
	b.SetUint32(uint32(req.Uint32))
	b.SetUint64(uint64(req.Uint64))
	b.SetFloat32(req.Float32)
	b.SetFloat64(req.Float64)
	b.SetStringType(req.StringType)
	b.SetBool(req.Bool)
	b.SetUUID(req.UUID)
	b.SetTime(req.Time)
	b.SetText(req.Text)
	b.SetState(alltypes.State(req.State))
	b.SetBytes(req.Bytes)
	b.SetNilable(req.Nilable)
	// Add all edges.
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.AllTypes.Query().Where(alltypes.ID(e.ID))
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewAllTypesCreate(e), nil
}

// ReadAllTypes handles GET /all-types-slice/{id} requests.
func (h *OgentHandler) ReadAllTypes(ctx context.Context, params ReadAllTypesParams) (ReadAllTypesRes, error) {
	q := h.client.AllTypes.Query().Where(alltypes.IDEQ(uint32(params.ID)))
	e, err := q.Only(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return NewAllTypesRead(e), nil
}

// UpdateAllTypes handles PATCH /all-types-slice/{id} requests.
func (h *OgentHandler) UpdateAllTypes(ctx context.Context, req *UpdateAllTypesReq, params UpdateAllTypesParams) (UpdateAllTypesRes, error) {
	b := h.client.AllTypes.UpdateOneID(uint32(params.ID))
	// Add all fields.
	if v, ok := req.Int.Get(); ok {
		b.SetInt(v)
	}
	if v, ok := req.Int8.Get(); ok {
		b.SetInt8(int8(v))
	}
	if v, ok := req.Int16.Get(); ok {
		b.SetInt16(int16(v))
	}
	if v, ok := req.Int32.Get(); ok {
		b.SetInt32(v)
	}
	if v, ok := req.Int64.Get(); ok {
		b.SetInt64(v)
	}
	if v, ok := req.Uint.Get(); ok {
		b.SetUint(uint(v))
	}
	if v, ok := req.Uint8.Get(); ok {
		b.SetUint8(uint8(v))
	}
	if v, ok := req.Uint16.Get(); ok {
		b.SetUint16(uint16(v))
	}
	if v, ok := req.Uint32.Get(); ok {
		b.SetUint32(uint32(v))
	}
	if v, ok := req.Uint64.Get(); ok {
		b.SetUint64(uint64(v))
	}
	if v, ok := req.Float32.Get(); ok {
		b.SetFloat32(v)
	}
	if v, ok := req.Float64.Get(); ok {
		b.SetFloat64(v)
	}
	if v, ok := req.StringType.Get(); ok {
		b.SetStringType(v)
	}
	if v, ok := req.Bool.Get(); ok {
		b.SetBool(v)
	}
	if v, ok := req.UUID.Get(); ok {
		b.SetUUID(v)
	}
	if v, ok := req.Time.Get(); ok {
		b.SetTime(v)
	}
	if v, ok := req.Text.Get(); ok {
		b.SetText(v)
	}
	if v, ok := req.State.Get(); ok {
		b.SetState(alltypes.State(v))
	}
	if req.Bytes != nil {
		b.SetBytes(req.Bytes)
	}
	if v, ok := req.Nilable.Get(); ok {
		b.SetNilable(v)
	}
	// Add all edges.
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.AllTypes.Query().Where(alltypes.ID(e.ID))
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewAllTypesUpdate(e), nil
}

// DeleteAllTypes handles DELETE /all-types-slice/{id} requests.
func (h *OgentHandler) DeleteAllTypes(ctx context.Context, params DeleteAllTypesParams) (DeleteAllTypesRes, error) {
	err := h.client.AllTypes.DeleteOneID(uint32(params.ID)).Exec(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return new(DeleteAllTypesNoContent), nil

}

// ListAllTypes handles GET /all-types-slice requests.
func (h *OgentHandler) ListAllTypes(ctx context.Context, params ListAllTypesParams) (ListAllTypesRes, error) {
	q := h.client.AllTypes.Query()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)

	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewAllTypesLists(es)
	return (*ListAllTypesOKApplicationJSON)(&r), nil
}

// CreateCategory handles POST /categories requests.
func (h *OgentHandler) CreateCategory(ctx context.Context, req *CreateCategoryReq) (CreateCategoryRes, error) {
	b := h.client.Category.Create()
	// Add all fields.
	b.SetName(req.Name)
	// Add all edges.
	b.AddPetIDs(req.Pets...)
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.Category.Query().Where(category.ID(e.ID))
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewCategoryCreate(e), nil
}

// ReadCategory handles GET /categories/{id} requests.
func (h *OgentHandler) ReadCategory(ctx context.Context, params ReadCategoryParams) (ReadCategoryRes, error) {
	q := h.client.Category.Query().Where(category.IDEQ(params.ID))
	e, err := q.Only(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return NewCategoryRead(e), nil
}

// UpdateCategory handles PATCH /categories/{id} requests.
func (h *OgentHandler) UpdateCategory(ctx context.Context, req *UpdateCategoryReq, params UpdateCategoryParams) (UpdateCategoryRes, error) {
	b := h.client.Category.UpdateOneID(params.ID)
	// Add all fields.
	if v, ok := req.Name.Get(); ok {
		b.SetName(v)
	}
	// Add all edges.
	if req.Pets != nil {
		b.ClearPets().AddPetIDs(req.Pets...)
	}
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.Category.Query().Where(category.ID(e.ID))
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewCategoryUpdate(e), nil
}

// DeleteCategory handles DELETE /categories/{id} requests.
func (h *OgentHandler) DeleteCategory(ctx context.Context, params DeleteCategoryParams) (DeleteCategoryRes, error) {
	err := h.client.Category.DeleteOneID(params.ID).Exec(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return new(DeleteCategoryNoContent), nil

}

// ListCategory handles GET /categories requests.
func (h *OgentHandler) ListCategory(ctx context.Context, params ListCategoryParams) (ListCategoryRes, error) {
	q := h.client.Category.Query()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)

	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewCategoryLists(es)
	return (*ListCategoryOKApplicationJSON)(&r), nil
}

// ListCategoryPets handles GET /categories/{id}/pets requests.
func (h *OgentHandler) ListCategoryPets(ctx context.Context, params ListCategoryPetsParams) (ListCategoryPetsRes, error) {
	q := h.client.Category.Query().Where(category.IDEQ(params.ID)).QueryPets()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)
	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewCategoryPetsLists(es)
	return (*ListCategoryPetsOKApplicationJSON)(&r), nil
}

// CreateHat handles POST /hats requests.
func (h *OgentHandler) CreateHat(ctx context.Context, req *CreateHatReq) (CreateHatRes, error) {
	b := h.client.Hat.Create()
	// Add all fields.
	b.SetName(req.Name)
	b.SetType(hat.Type(req.Type))
	// Add all edges.
	if v, ok := req.Wearer.Get(); ok {
		b.SetWearerID(v)
	}
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.Hat.Query().Where(hat.ID(e.ID))
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewHatCreate(e), nil
}

// ReadHat handles GET /hats/{id} requests.
func (h *OgentHandler) ReadHat(ctx context.Context, params ReadHatParams) (ReadHatRes, error) {
	q := h.client.Hat.Query().Where(hat.IDEQ(params.ID))
	e, err := q.Only(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return NewHatRead(e), nil
}

// UpdateHat handles PATCH /hats/{id} requests.
func (h *OgentHandler) UpdateHat(ctx context.Context, req *UpdateHatReq, params UpdateHatParams) (UpdateHatRes, error) {
	b := h.client.Hat.UpdateOneID(params.ID)
	// Add all fields.
	if v, ok := req.Name.Get(); ok {
		b.SetName(v)
	}
	// Add all edges.
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.Hat.Query().Where(hat.ID(e.ID))
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewHatUpdate(e), nil
}

// DeleteHat handles DELETE /hats/{id} requests.
func (h *OgentHandler) DeleteHat(ctx context.Context, params DeleteHatParams) (DeleteHatRes, error) {
	err := h.client.Hat.DeleteOneID(params.ID).Exec(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return new(DeleteHatNoContent), nil

}

// ListHat handles GET /hats requests.
func (h *OgentHandler) ListHat(ctx context.Context, params ListHatParams) (ListHatRes, error) {
	q := h.client.Hat.Query()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)

	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewHatLists(es)
	return (*ListHatOKApplicationJSON)(&r), nil
}

// ReadHatWearer handles GET /hats/{id}/wearer requests.
func (h *OgentHandler) ReadHatWearer(ctx context.Context, params ReadHatWearerParams) (ReadHatWearerRes, error) {
	q := h.client.Hat.Query().Where(hat.IDEQ(params.ID)).QueryWearer()
	e, err := q.Only(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return NewHatWearerRead(e), nil
}

// CreatePet handles POST /pets requests.
func (h *OgentHandler) CreatePet(ctx context.Context, req *CreatePetReq) (CreatePetRes, error) {
	b := h.client.Pet.Create()
	// Add all fields.
	b.SetName(req.Name)
	if v, ok := req.Weight.Get(); ok {
		b.SetWeight(v)
	}
	if v, ok := req.Birthday.Get(); ok {
		b.SetBirthday(v)
	}
	if req.TagID != nil {
		b.SetTagID(req.TagID)
	}
	if v, ok := req.Height.Get(); ok {
		b.SetHeight(v)
	}
	// Add all edges.
	b.AddCategoryIDs(req.Categories...)
	b.SetOwnerID(req.Owner)
	b.AddRescuerIDs(req.Rescuer...)
	b.AddFriendIDs(req.Friends...)
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.Pet.Query().Where(pet.ID(e.ID))
	// Eager load edges that are required on create operation.
	q.WithCategories().WithOwner()
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewPetCreate(e), nil
}

// DeletePet handles DELETE /pets/{id} requests.
func (h *OgentHandler) DeletePet(ctx context.Context, params DeletePetParams) (DeletePetRes, error) {
	err := h.client.Pet.DeleteOneID(params.ID).Exec(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return new(DeletePetNoContent), nil

}

// ListPet handles GET /pets requests.
func (h *OgentHandler) ListPet(ctx context.Context, params ListPetParams) (ListPetRes, error) {
	q := h.client.Pet.Query()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)

	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewPetLists(es)
	return (*ListPetOKApplicationJSON)(&r), nil
}

// ReadPet handles GET /pets/{id} requests.
func (h *OgentHandler) ReadPet(ctx context.Context, params ReadPetParams) (ReadPetRes, error) {
	q := h.client.Pet.Query().Where(pet.IDEQ(params.ID))
	e, err := q.Only(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return NewPetRead(e), nil
}

// UpdatePet handles PATCH /pets/{id} requests.
func (h *OgentHandler) UpdatePet(ctx context.Context, req *UpdatePetReq, params UpdatePetParams) (UpdatePetRes, error) {
	b := h.client.Pet.UpdateOneID(params.ID)
	// Add all fields.
	if v, ok := req.Name.Get(); ok {
		b.SetName(v)
	}
	if v, ok := req.Weight.Get(); ok {
		b.SetWeight(v)
	}
	if v, ok := req.Birthday.Get(); ok {
		b.SetBirthday(v)
	}
	if req.TagID != nil {
		b.SetTagID(req.TagID)
	}
	if v, ok := req.Height.Get(); ok {
		b.SetHeight(v)
	}
	// Add all edges.
	if req.Categories != nil {
		b.ClearCategories().AddCategoryIDs(req.Categories...)
	}
	if v, ok := req.Owner.Get(); ok {
		b.SetOwnerID(v)
	}
	if req.Friends != nil {
		b.ClearFriends().AddFriendIDs(req.Friends...)
	}
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.Pet.Query().Where(pet.ID(e.ID))
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewPetUpdate(e), nil
}

// ListPetCategories handles GET /pets/{id}/categories requests.
func (h *OgentHandler) ListPetCategories(ctx context.Context, params ListPetCategoriesParams) (ListPetCategoriesRes, error) {
	q := h.client.Pet.Query().Where(pet.IDEQ(params.ID)).QueryCategories()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)
	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewPetCategoriesLists(es)
	return (*ListPetCategoriesOKApplicationJSON)(&r), nil
}

// ReadPetOwner handles GET /pets/{id}/owner requests.
func (h *OgentHandler) ReadPetOwner(ctx context.Context, params ReadPetOwnerParams) (ReadPetOwnerRes, error) {
	q := h.client.Pet.Query().Where(pet.IDEQ(params.ID)).QueryOwner()
	e, err := q.Only(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return NewPetOwnerRead(e), nil
}

// ListPetRescuer handles GET /pets/{id}/rescuer requests.
func (h *OgentHandler) ListPetRescuer(ctx context.Context, params ListPetRescuerParams) (ListPetRescuerRes, error) {
	q := h.client.Pet.Query().Where(pet.IDEQ(params.ID)).QueryRescuer()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)
	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewPetRescuerLists(es)
	return (*ListPetRescuerOKApplicationJSON)(&r), nil
}

// ListPetFriends handles GET /pets/{id}/friends requests.
func (h *OgentHandler) ListPetFriends(ctx context.Context, params ListPetFriendsParams) (ListPetFriendsRes, error) {
	q := h.client.Pet.Query().Where(pet.IDEQ(params.ID)).QueryFriends()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)
	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewPetFriendsLists(es)
	return (*ListPetFriendsOKApplicationJSON)(&r), nil
}

// CreateUser handles POST /users requests.
func (h *OgentHandler) CreateUser(ctx context.Context, req *CreateUserReq) (CreateUserRes, error) {
	b := h.client.User.Create()
	// Add all fields.
	b.SetName(req.Name)
	b.SetAge(uint(req.Age))
	if v, ok := req.Height.Get(); ok {
		b.SetHeight(uint(v))
	}
	b.SetFavoriteCatBreed(user.FavoriteCatBreed(req.FavoriteCatBreed))
	b.SetFavoriteColor(user.FavoriteColor(req.FavoriteColor))
	if v, ok := req.FavoriteDogBreed.Get(); ok {
		b.SetFavoriteDogBreed(user.FavoriteDogBreed(v))
	}
	if v, ok := req.FavoriteFishBreed.Get(); ok {
		b.SetFavoriteFishBreed(schema.FishBreed(v))
	}
	// Add all edges.
	b.AddPetIDs(req.Pets...)
	b.AddAnimalsSavedIDs(req.AnimalsSaved...)
	if v, ok := req.BestFriend.Get(); ok {
		b.SetBestFriendID(v)
	}
	if v, ok := req.FavoriteHat.Get(); ok {
		b.SetFavoriteHatID(v)
	}
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.User.Query().Where(user.ID(e.ID))
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewUserCreate(e), nil
}

// ReadUser handles GET /users/{id} requests.
func (h *OgentHandler) ReadUser(ctx context.Context, params ReadUserParams) (ReadUserRes, error) {
	q := h.client.User.Query().Where(user.IDEQ(params.ID))
	e, err := q.Only(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return NewUserRead(e), nil
}

// UpdateUser handles PATCH /users/{id} requests.
func (h *OgentHandler) UpdateUser(ctx context.Context, req *UpdateUserReq, params UpdateUserParams) (UpdateUserRes, error) {
	b := h.client.User.UpdateOneID(params.ID)
	// Add all fields.
	if v, ok := req.Name.Get(); ok {
		b.SetName(v)
	}
	if v, ok := req.Age.Get(); ok {
		b.SetAge(uint(v))
	}
	if v, ok := req.Height.Get(); ok {
		b.SetHeight(uint(v))
	}
	if v, ok := req.FavoriteCatBreed.Get(); ok {
		b.SetFavoriteCatBreed(user.FavoriteCatBreed(v))
	}
	if v, ok := req.FavoriteDogBreed.Get(); ok {
		b.SetFavoriteDogBreed(user.FavoriteDogBreed(v))
	}
	if v, ok := req.FavoriteFishBreed.Get(); ok {
		b.SetFavoriteFishBreed(schema.FishBreed(v))
	}
	// Add all edges.
	if req.Pets != nil {
		b.ClearPets().AddPetIDs(req.Pets...)
	}
	if req.AnimalsSaved != nil {
		b.ClearAnimalsSaved().AddAnimalsSavedIDs(req.AnimalsSaved...)
	}
	if v, ok := req.BestFriend.Get(); ok {
		b.SetBestFriendID(v)
	}
	// Persist to storage.
	e, err := b.Save(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	// Reload the entity to attach all eager-loaded edges.
	q := h.client.User.Query().Where(user.ID(e.ID))
	e, err = q.Only(ctx)
	if err != nil {
		// This should never happen.
		return nil, err
	}
	return NewUserUpdate(e), nil
}

// DeleteUser handles DELETE /users/{id} requests.
func (h *OgentHandler) DeleteUser(ctx context.Context, params DeleteUserParams) (DeleteUserRes, error) {
	err := h.client.User.DeleteOneID(params.ID).Exec(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsConstraintError(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return new(DeleteUserNoContent), nil

}

// ListUser handles GET /users requests.
func (h *OgentHandler) ListUser(ctx context.Context, params ListUserParams) (ListUserRes, error) {
	q := h.client.User.Query()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)

	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewUserLists(es)
	return (*ListUserOKApplicationJSON)(&r), nil
}

// ListUserPets handles GET /users/{id}/pets requests.
func (h *OgentHandler) ListUserPets(ctx context.Context, params ListUserPetsParams) (ListUserPetsRes, error) {
	q := h.client.User.Query().Where(user.IDEQ(params.ID)).QueryPets()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)
	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewUserPetsLists(es)
	return (*ListUserPetsOKApplicationJSON)(&r), nil
}

// ListUserAnimalsSaved handles GET /users/{id}/animals-saved requests.
func (h *OgentHandler) ListUserAnimalsSaved(ctx context.Context, params ListUserAnimalsSavedParams) (ListUserAnimalsSavedRes, error) {
	q := h.client.User.Query().Where(user.IDEQ(params.ID)).QueryAnimalsSaved()
	page := 1
	if v, ok := params.Page.Get(); ok {
		page = v
	}
	itemsPerPage := 30
	if v, ok := params.ItemsPerPage.Get(); ok {
		itemsPerPage = v
	}
	q.Limit(itemsPerPage).Offset((page - 1) * itemsPerPage)
	es, err := q.All(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	r := NewUserAnimalsSavedLists(es)
	return (*ListUserAnimalsSavedOKApplicationJSON)(&r), nil
}

// ReadUserBestFriend handles GET /users/{id}/best-friend requests.
func (h *OgentHandler) ReadUserBestFriend(ctx context.Context, params ReadUserBestFriendParams) (ReadUserBestFriendRes, error) {
	q := h.client.User.Query().Where(user.IDEQ(params.ID)).QueryBestFriend()
	e, err := q.Only(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return NewUserBestFriendRead(e), nil
}

// ReadUserFavoriteHat handles GET /users/{id}/favorite-hat requests.
func (h *OgentHandler) ReadUserFavoriteHat(ctx context.Context, params ReadUserFavoriteHatParams) (ReadUserFavoriteHatRes, error) {
	q := h.client.User.Query().Where(user.IDEQ(params.ID)).QueryFavoriteHat()
	e, err := q.Only(ctx)
	if err != nil {
		switch {
		case ent.IsNotFound(err):
			return &R404{
				Code:   http.StatusNotFound,
				Status: http.StatusText(http.StatusNotFound),
				Errors: rawError(err),
			}, nil
		case ent.IsNotSingular(err):
			return &R409{
				Code:   http.StatusConflict,
				Status: http.StatusText(http.StatusConflict),
				Errors: rawError(err),
			}, nil
		default:
			// Let the server handle the error.
			return nil, err
		}
	}
	return NewUserFavoriteHatRead(e), nil
}
