<commit_before>package models

import (
	"strconv"
	"errors"

	"github.com/dmitry-kuchura/access-application/app"
)

const (
	insertDomain = `
	INSERT INTO domains (name, url, description, status, created_at, updated_at)
	VALUES(?, ?, ?, 1, NOW(), NOW()) ON DUPLICATE KEY UPDATE
	name = VALUES(name)
	`

	checkDomain = `
	SELECT COUNT(*) as count FROM domains WHERE name LIKE ? OR url LIKE ?
	`

	selectAllDomains = `
	SELECT id, name, url, status, updated_at
	FROM domains
	LIMIT ?
	OFFSET ?
	`
	countAllDomains = `
	SELECT COUNT(*) as count
	FROM domains
	`

	deleteDomain = `
	DELETE FROM domains WHERE id = ?
	`

	selectDomain = `
	SELECT id, name, url, status, updated_at FROM domains WHERE id = ?
	`
)

type Domain struct {
	ID          int        `form:"id" json:"id"`
	Name        string     `form:"name" json:"name"`
	Url         string     `form:"url" json:"url"`
	Description string     `form:"description" json:"description"`
	Status      int        `form:"status" json:"status"`
	Updated     string     `form:"updated_at" json:"updated_at"`
	Ftps        []Ftp      `form:"ftp" json:"ftp"`
	Databases   []Database `form:"database" json:"database"`
	Hostings    []Hosting  `form:"hosting" json:"hosting"`
	Admins      []Admin    `form:"admin" json:"admin"`
}

// Добавление домена
func CreateDomain(name, url, description string) (string, error) {
	if CheckDomain(name, url) {
		res, err := app.Exec(insertDomain, name, url, description)

		id, err := res.LastInsertId()
		if err != nil {
			return "", err
		}
		return strconv.FormatInt(id, 10), nil
	} else {
		err := errors.New("Domain was already created!")
		return "", err
	}
}

// Список доменов
func AllDomains(param string) (domains []Domain, count int, err error) {
	limit := 15
	page, _ := strconv.Atoi(param)
	offset := (page - 1) * limit

	rows, err := app.Query(selectAllDomains, limit, offset)

	row, _ := app.Query(countAllDomains)

	allDomain := app.CountRows(row)<commit_msg>Update domains.go<commit_after>package models

import (
	"strconv"
	"errors"

	"github.com/dmitry-kuchura/access-application/app"
)

const (
	insertDomain = `
	INSERT INTO domains (name, url, description, status, created_at, updated_at)
	VALUES(?, ?, ?, 1, NOW(), NOW()) ON DUPLICATE KEY UPDATE
	name = VALUES(name)
	`

	checkDomain = `
	SELECT COUNT(*) as count FROM domains WHERE name LIKE ? OR url LIKE ?
	`

	selectAllDomains = `
	SELECT id, name, url, status, updated_at
	FROM domains
	LIMIT ?
	OFFSET ?
	`
	countAllDomains = `
	SELECT COUNT(*) as count
	FROM domains
	`

	deleteDomain = `
	DELETE FROM domains WHERE id = ?
	`

	selectDomain = `
	SELECT id, name, url, status, updated_at FROM domains WHERE id = ?
	`
)

type Domain struct {
	ID          int        `form:"id" json:"id"`
	Name        string     `form:"name" json:"name"`
	Url         string     `form:"url" json:"url"`
	Description string     `form:"description" json:"description"`
	Status      int        `form:"status" json:"status"`
	Updated     string     `form:"updated_at" json:"updated_at"`
	Ftps        []Ftp      `form:"ftp" json:"ftp"`
	Databases   []Database `form:"database" json:"database"`
	Hostings    []Hosting  `form:"hosting" json:"hosting"`
	Admins      []Admin    `form:"admin" json:"admin"`
}

type DomainInterface interface {
	Domain(id int) (*Domain, error)
	CreateDomain(name, url, description string) (string, error)
	AllDomains(param string) (domains []Domain, count int, err error)
	DeleteDomain(id int) (bool, error)
	GetDomain(param int) (domains []Domain, err error)
}

// Добавление домена
func CreateDomain(name, url, description string) (string, error) {
	if CheckDomain(name, url) {
		res, err := app.Exec(insertDomain, name, url, description)

		id, err := res.LastInsertId()
		if err != nil {
			return "", err
		}
		return strconv.FormatInt(id, 10), nil
	} else {
		err := errors.New("Domain was already created!")
		return "", err
	}
}

// Список доменов
func AllDomains(param string) (domains []Domain, count int, err error) {
	limit := 15
	page, _ := strconv.Atoi(param)
	offset := (page - 1) * limit

	rows, err := app.Query(selectAllDomains, limit, offset)

	row, _ := app.Query(countAllDomains)

	allDomain := app.CountRows(row)
{'fraction_non_alphanumeric': 0.13867047891350964, 'fraction_numerical': 0.0028591851322373124, 'mean_word_length': 3.6541019955654104, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '64138', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1417, 'n_words': 399}