<commit_before>package routes

import (
	"time"

	"ireul.com/bastion/models"
	"ireul.com/web"
)

// GrantList list all grants
func GrantList(ctx *web.Context, r APIRender, db *models.DB) {
	gs := []models.Grant{}
	db.Find(&gs)
	r.Success("grants", models.ConvertGrantResolved(gs))
}

// GrantCreateForm from for create grant
type GrantCreateForm struct {
	UserID    uint   `json:"userId"`
	Tag       string `json:"tag"`
	CanSudo   bool   `json:"canSudo"`
	ExpiresAt int64  `json:"expiresAt"`
}

// ExpiresAtTime convert ExpiresAt to *time.Time
func (f GrantCreateForm) ExpiresAtTime() *time.Time {
	// convert to time.Time
	var t *time.Time
	if f.ExpiresAt != 0 {
		*t = time.Unix(f.ExpiresAt, 0)
	}
	return t
}

// GrantCreate create/update a grant
func GrantCreate(ctx *web.Context, r APIRender, db *models.DB, f GrantCreateForm) {
	g := &models.Grant{}
	db.Where("user_id = ? AND tag = ?", f.UserID, f.Tag).First(g)
	if db.NewRecord(g) {
		*g = models.Grant{
			UserID:    f.UserID,
			Tag:       f.Tag,
			CanSudo:   f.CanSudo,
			ExpiresAt: f.ExpiresAtTime(),
		}
		db.Create(g)
	} else {
		db.Model(g).Update(map[string]interface{}{"CanSudo": f.CanSudo, "ExpiresAt": f.ExpiresAtTime()})
	}
	r.Success("grant", g)
}

// GrantDestroy update a grant
func GrantDestroy(ctx *web.Context, r APIRender, db *models.DB) {
	id := ctx.ParamsInt(":id")
	db.Unscoped().Where("id = ?", id).Delete(&models.Grant{})
	r.Success()
}<commit_msg>using ExpiresIn for time create/update<commit_after>package routes

import (
	"time"

	"ireul.com/bastion/models"
	"ireul.com/web"
)

// GrantList list all grants
func GrantList(ctx *web.Context, r APIRender, db *models.DB) {
	gs := []models.Grant{}
	db.Find(&gs)
	r.Success("grants", models.ConvertGrantResolved(gs))
}

// GrantCreateForm from for create grant
type GrantCreateForm struct {
	UserID    uint   `json:"userId"`
	Tag       string `json:"tag"`
	CanSudo   bool   `json:"canSudo"`
	ExpiresIn int64  `json:"expiresIn"`
}

// ExpiresAt convert ExpiresAt to *time.Time
func (f GrantCreateForm) ExpiresAt() *time.Time {
	// convert to time.Time
	var t *time.Time
	if f.ExpiresIn != 0 {
		*t = time.Now().Add(time.Second * time.Duration(f.ExpiresIn))
	}
	return t
}

// GrantCreate create/update a grant
func GrantCreate(ctx *web.Context, r APIRender, db *models.DB, f GrantCreateForm) {
	g := &models.Grant{}
	db.Where("user_id = ? AND tag = ?", f.UserID, f.Tag).First(g)
	if db.NewRecord(g) {
		*g = models.Grant{
			UserID:    f.UserID,
			Tag:       f.Tag,
			CanSudo:   f.CanSudo,
			ExpiresAt: f.ExpiresAt(),
		}
		db.Create(g)
	} else {
		db.Model(g).Update(map[string]interface{}{"CanSudo": f.CanSudo, "ExpiresAt": f.ExpiresAt()})
	}
	r.Success("grant", g)
}

// GrantDestroy update a grant
func GrantDestroy(ctx *web.Context, r APIRender, db *models.DB) {
	id := ctx.ParamsInt(":id")
	db.Unscoped().Where("id = ?", id).Delete(&models.Grant{})
	r.Success()
}
{'fraction_non_alphanumeric': 0.1652892561983471, 'fraction_numerical': 0.0024104683195592287, 'mean_word_length': 3.8255813953488373, 'pattern_counts': {'":': 6, '<': 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': '623771', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1126, 'n_words': 248}