// Code generated by sqlc. DO NOT EDIT.
// versions:
//   sqlc v1.28.0
// source: district.sql

package db

import (
	"context"
)

const allDistricts = `-- name: AllDistricts :many
SELECT id, name, code, created_at FROM districts
`

func (q *Queries) AllDistricts(ctx context.Context) ([]District, error) {
	rows, err := q.db.QueryContext(ctx, allDistricts)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []District{}
	for rows.Next() {
		var i District
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Code,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const createDistrict = `-- name: CreateDistrict :one
INSERT INTO districts (
    name,
    code
 ) VALUES (
    $1, $2
 ) RETURNING id, name, code, created_at
`

type CreateDistrictParams struct {
	Name string `json:"name"`
	Code string `json:"code"`
}

func (q *Queries) CreateDistrict(ctx context.Context, arg CreateDistrictParams) (District, error) {
	row := q.db.QueryRowContext(ctx, createDistrict, arg.Name, arg.Code)
	var i District
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Code,
		&i.CreatedAt,
	)
	return i, err
}

const deleteDisrict = `-- name: DeleteDisrict :exec
DELETE FROM districts WHERE id= $1
`

func (q *Queries) DeleteDisrict(ctx context.Context, id int64) error {
	_, err := q.db.ExecContext(ctx, deleteDisrict, id)
	return err
}

const getDistrict = `-- name: GetDistrict :one
SELECT id, name, code, created_at FROM districts
WHERE id = $1 LIMIT 1
`

func (q *Queries) GetDistrict(ctx context.Context, id int64) (District, error) {
	row := q.db.QueryRowContext(ctx, getDistrict, id)
	var i District
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Code,
		&i.CreatedAt,
	)
	return i, err
}

const getDistrictByName = `-- name: GetDistrictByName :one
SELECT id, name, code, created_at FROM districts
WHERE name = $1 LIMIT 1
`

func (q *Queries) GetDistrictByName(ctx context.Context, name string) (District, error) {
	row := q.db.QueryRowContext(ctx, getDistrictByName, name)
	var i District
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Code,
		&i.CreatedAt,
	)
	return i, err
}

const getDistrictIDByCode = `-- name: GetDistrictIDByCode :one
SELECT id from districts WHERE 
code= $1 LIMIT 1
`

func (q *Queries) GetDistrictIDByCode(ctx context.Context, code string) (int64, error) {
	row := q.db.QueryRowContext(ctx, getDistrictIDByCode, code)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const getDistrictIdByName = `-- name: GetDistrictIdByName :one
SELECT id FROM districts
WHERE name = $1 LIMIT 1
`

func (q *Queries) GetDistrictIdByName(ctx context.Context, name string) (int64, error) {
	row := q.db.QueryRowContext(ctx, getDistrictIdByName, name)
	var id int64
	err := row.Scan(&id)
	return id, err
}

const listDistricts = `-- name: ListDistricts :many
SELECT id, name, code, created_at from districts
ORDER BY id LIMIT $1
OFFSET $2
`

type ListDistrictsParams struct {
	Limit  int32 `json:"limit"`
	Offset int32 `json:"offset"`
}

func (q *Queries) ListDistricts(ctx context.Context, arg ListDistrictsParams) ([]District, error) {
	rows, err := q.db.QueryContext(ctx, listDistricts, arg.Limit, arg.Offset)
	if err != nil {
		return nil, err
	}
	defer rows.Close()
	items := []District{}
	for rows.Next() {
		var i District
		if err := rows.Scan(
			&i.ID,
			&i.Name,
			&i.Code,
			&i.CreatedAt,
		); err != nil {
			return nil, err
		}
		items = append(items, i)
	}
	if err := rows.Close(); err != nil {
		return nil, err
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return items, nil
}

const updateDistrict = `-- name: UpdateDistrict :one
UPDATE districts
  set 
    name = $2,
    code = $3
WHERE id = $1
RETURNING id, name, code, created_at
`

type UpdateDistrictParams struct {
	ID   int64  `json:"id"`
	Name string `json:"name"`
	Code string `json:"code"`
}

func (q *Queries) UpdateDistrict(ctx context.Context, arg UpdateDistrictParams) (District, error) {
	row := q.db.QueryRowContext(ctx, updateDistrict, arg.ID, arg.Name, arg.Code)
	var i District
	err := row.Scan(
		&i.ID,
		&i.Name,
		&i.Code,
		&i.CreatedAt,
	)
	return i, err
}
