1
0
api/main.go

91 lines
2.2 KiB
Go
Raw Normal View History

2022-08-31 06:18:14 +00:00
package main
import (
"context"
"log"
2022-08-31 07:49:08 +00:00
"net/http"
2024-02-21 09:16:45 +00:00
"api/ent"
"api/ent/ogent"
2022-08-31 07:49:08 +00:00
"entgo.io/ent/dialect"
_ "github.com/mattn/go-sqlite3"
2023-04-05 06:05:14 +00:00
"entgo.io/ent/dialect/sql/schema"
2022-08-31 07:49:08 +00:00
"time"
2022-08-31 06:18:14 +00:00
)
type User struct {
2022-08-31 07:49:08 +00:00
username string `json:"username"`
2022-08-31 06:18:14 +00:00
created_at time.Time `json:"created_at"`
}
type handler struct {
*ogent.OgentHandler
client *ent.Client
}
2022-08-31 07:49:08 +00:00
func (h handler) DrawStart(ctx context.Context, params ogent.DrawStartParams) error {
error := h.client.Card.UpdateOneID(params.ID).Exec(ctx)
return (error)
2022-08-31 06:18:14 +00:00
}
2022-08-31 07:49:08 +00:00
//func (h handler) DrawDone(ctx context.Context, params ogent.DrawDoneParams) (ogent.DrawDoneNoContent, error) {
func (h handler) DrawDone(ctx context.Context, params ogent.DrawDoneParams) error {
body := h.client.Card.GetX(ctx, params.ID)
u_body := h.client.User.GetX(ctx, params.ID)
2022-08-31 06:18:14 +00:00
jst, err := time.LoadLocation("Asia/Tokyo")
if err != nil {
panic(err)
}
t := time.Now().In(jst)
tt := t.Format("20060102")
2022-08-31 07:49:08 +00:00
f := body.CreatedAt.Add(time.Hour * 24 * 1).In(jst)
2022-08-31 06:18:14 +00:00
ff := f.Format("20060102")
2022-08-31 07:49:08 +00:00
fff := u_body.Next
2022-08-31 06:18:14 +00:00
if tt < fff {
2022-08-31 07:49:08 +00:00
error := h.client.User.UpdateOneID(params.ID).SetNext(ff).Exec(ctx)
return (error)
2022-08-31 06:18:14 +00:00
}
2022-08-31 07:49:08 +00:00
error := h.client.User.UpdateOneID(params.ID).SetUpdatedAt(t).Exec(ctx)
return (error)
2022-08-31 06:18:14 +00:00
}
func main() {
2022-08-31 07:49:08 +00:00
// Create ent client.
2024-02-26 10:29:51 +00:00
//// sqlite
//url := "file:/app/data/new.sqlite?_fk=1"
//client, err := ent.Open(dialect.SQLite, url)
////client, err := ent.Open(dialect.SQLite, "file:data?mode=memory&cache=shared&_fk=1")
// postgres
//url := os.Getenv("DATABASE_URL") + "?sslmode=require"
url := "postgres://api:api@database/api"
client, err := ent.Open("postgres", url)
2022-08-31 07:49:08 +00:00
if err != nil {
2022-08-31 06:18:14 +00:00
log.Fatal(err)
}
2022-08-31 07:49:08 +00:00
// Run the migrations.
2024-02-26 10:29:51 +00:00
if err := client.Schema.Create(context.Background()); err != nil {
log.Fatal(err)
}
2023-04-05 06:05:14 +00:00
ctx := context.Background()
err = client.Schema.Create(ctx, schema.WithAtlas(true))
if err != nil {
log.Fatalf("failed creating schema resources: %v", err)
2022-08-31 06:18:14 +00:00
}
2022-08-31 07:49:08 +00:00
// Create the handler.
2022-08-31 06:18:14 +00:00
h := handler{
OgentHandler: ogent.NewOgentHandler(client),
client: client,
}
2022-08-31 07:49:08 +00:00
// Start listening.
srv, err := ogent.NewServer(h)
2022-08-31 06:18:14 +00:00
//srv,err := ogent.NewServer(ogent.NewOgentHandler(client))
if err != nil {
log.Fatal(err)
}
2022-08-31 07:49:08 +00:00
if err := http.ListenAndServe(":8080", srv); err != nil {
2022-08-31 06:18:14 +00:00
log.Fatal(err)
}
}