Compare commits

1 Commits

Author SHA1 Message Date
kandrusyak
2916a50336 swagger 2023-07-16 23:32:02 +03:00
24 changed files with 363 additions and 835 deletions

1
.gitignore vendored
View File

@@ -1,4 +1,3 @@
.idea
build
.env
test

View File

@@ -1,4 +1,4 @@
FROM docker.io/kandrusyak/go-build as build
FROM kandrusyak/go-build as build
LABEL authors="andrusyakka@dopcore.com"
WORKDIR /app
@@ -13,7 +13,7 @@ COPY . ./
RUN go build -o ./app
FROM docker.io/alpine:edge
FROM alpine:edge
WORKDIR /

View File

@@ -10,8 +10,7 @@ type ServerConfig struct {
}
type MSHosts struct {
UsersHost string
PersonsHost string
UsersHost string
}
type Config struct {
@@ -26,8 +25,7 @@ func New() *Config {
ConnectionString: getEnv("CONNECTION_STRING", "host=localhost user=astra_events password=password dbname=astra_events_db port=5432 sslmode=disable TimeZone=Europe/Moscow"),
},
MSHosts: MSHosts{
UsersHost: getEnv("ASTRA-USERS", "http://localhost:8083"),
PersonsHost: getEnv("ASTRA-PERSONAL-INFORMATION", "http://localhost:8082"),
UsersHost: getEnv("ASTRA-USERS", "http://localhost:8081"),
},
}
}

View File

@@ -1,2 +1,2 @@
SERVICE_REPLICAS=1
SERVICE_REPLICAS=3
SERVICE_PORT=8080

View File

@@ -2,6 +2,7 @@ apiVersion: v1
kind: Service
metadata:
name: {{ .Values.app.fullName }}
namespace: default
spec:
ports:
- port: {{ .Values.app.server.port }}

View File

@@ -1,6 +1,6 @@
version: "3.9"
services:
astra_events:
postgres:
image: postgres:14.8
environment:
POSTGRES_DB: "astra_events_db"

40
docs/docs.go Normal file
View File

@@ -0,0 +1,40 @@
// Code generated by swaggo/swag. DO NOT EDIT.
package docs
import "github.com/swaggo/swag"
const docTemplate = `{
"schemes": {{ marshal .Schemes }},
"swagger": "2.0",
"info": {
"description": "{{escape .Description}}",
"title": "{{.Title}}",
"contact": {
"name": "Kirill Andrusyak",
"email": "andrusyakka@dopcore.com"
},
"version": "{{.Version}}"
},
"host": "{{.Host}}",
"basePath": "{{.BasePath}}",
"paths": {}
}`
// SwaggerInfo holds exported Swagger Info so clients can modify it
var SwaggerInfo = &swag.Spec{
Version: "1.0",
Host: "api.astra.dev.dop",
BasePath: "/events",
Schemes: []string{},
Title: "Astra Events",
Description: "This is an astra-events service.",
InfoInstanceName: "swagger",
SwaggerTemplate: docTemplate,
LeftDelim: "{{",
RightDelim: "}}",
}
func init() {
swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo)
}

15
docs/swagger.json Normal file
View File

@@ -0,0 +1,15 @@
{
"swagger": "2.0",
"info": {
"description": "This is an astra-events service.",
"title": "Astra Events",
"contact": {
"name": "Kirill Andrusyak",
"email": "andrusyakka@dopcore.com"
},
"version": "1.0"
},
"host": "api.astra.dev.dop",
"basePath": "/events",
"paths": {}
}

11
docs/swagger.yaml Normal file
View File

@@ -0,0 +1,11 @@
basePath: /events
host: api.astra.dev.dop
info:
contact:
email: andrusyakka@dopcore.com
name: Kirill Andrusyak
description: This is an astra-events service.
title: Astra Events
version: "1.0"
paths: {}
swagger: "2.0"

51
go.mod
View File

@@ -3,33 +3,48 @@ module astra-events
go 1.21rc1
require (
github.com/go-playground/validator/v10 v10.14.1
github.com/google/uuid v1.3.0
github.com/hashicorp/go-set v0.1.13
github.com/labstack/echo/v4 v4.10.2
gorm.io/driver/postgres v1.5.2
gorm.io/gorm v1.25.2
)
require (
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/KyleBanks/depth v1.2.1 // indirect
github.com/PuerkitoBio/purell v1.2.0 // indirect
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.2 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/spec v0.20.9 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/hashicorp/go-set v0.1.13 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.3.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/labstack/echo/v4 v4.11.0 // indirect
github.com/labstack/gommon v0.4.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/swaggo/echo-swagger v1.4.0 // indirect
github.com/swaggo/files/v2 v2.0.0 // indirect
github.com/swaggo/swag v1.16.1 // indirect
github.com/urfave/cli/v2 v2.25.7 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.8.0 // indirect
golang.org/x/net v0.9.0 // indirect
golang.org/x/sys v0.7.0 // indirect
golang.org/x/text v0.9.0 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
golang.org/x/crypto v0.11.0 // indirect
golang.org/x/net v0.12.0 // indirect
golang.org/x/sys v0.10.0 // indirect
golang.org/x/text v0.11.0 // indirect
golang.org/x/time v0.3.0 // indirect
golang.org/x/tools v0.11.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
gorm.io/driver/postgres v1.5.2 // indirect
gorm.io/driver/sqlite v1.5.2 // indirect
gorm.io/gorm v1.25.2 // indirect
sigs.k8s.io/yaml v1.3.0 // indirect
)

140
go.sum
View File

@@ -1,22 +1,42 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/KyleBanks/depth v1.2.1 h1:5h8fQADFrWtarTdtDudMmGsC7GPbOAu6RVB3ffsVFHc=
github.com/KyleBanks/depth v1.2.1/go.mod h1:jzSb9d0L43HxTQfT+oSA1EEp2q+ne2uh6XgeJcm8brE=
github.com/PuerkitoBio/purell v1.1.1 h1:WEQqlqaGbrPkxLJWfBwQmfEAE1Z7ONdDLqrN38tNFfI=
github.com/PuerkitoBio/purell v1.1.1/go.mod h1:c11w/QuzBsJSee3cPx9rAFu61PvFxuPbtSwDGJws/X0=
github.com/PuerkitoBio/purell v1.2.0 h1:/Jdm5QfyM8zdlqT6WVZU4cfP23sot6CEHA4CS49Ezig=
github.com/PuerkitoBio/purell v1.2.0/go.mod h1:OhLRTaaIzhvIyofkJfB24gokC7tM42Px5UhoT32THBk=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578 h1:d+Bc7a5rLufV/sSk/8dngufqelfh6jnri85riMAaF/M=
github.com/PuerkitoBio/urlesc v0.0.0-20170810143723-de5bf2ad4578/go.mod h1:uGdkoq3SwY9Y+13GIhn11/XLaGBb4BfwItxLd5jeuXE=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.2 h1:p1EgwI/C7NhT0JmVkwCD2ZBK8j4aeHQX2pMHHBfMQ6w=
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gabriel-vasile/mimetype v1.4.2 h1:w5qFW6JKBz9Y393Y4q372O9A7cUSequkh1Q7OhCmWKU=
github.com/gabriel-vasile/mimetype v1.4.2/go.mod h1:zApsH/mKG4w07erKIaJPFiX0Tsq9BFQgN3qGY5GnNgA=
github.com/go-playground/assert/v2 v2.2.0 h1:JvknZsQTYeFEAhQwI4qEt9cyV5ONwRHC+lYKSsYSR8s=
github.com/go-playground/assert/v2 v2.2.0/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/oXslEjJA=
github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY=
github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY=
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator/v10 v10.14.1 h1:9c50NUPC30zyuKprjL3vNZ0m5oG+jU0zvx4AqHGnv4k=
github.com/go-playground/validator/v10 v10.14.1/go.mod h1:9iXMNT7sEkjXb0I+enO7QXmzG6QCsPWY4zveKFVRSyU=
github.com/go-openapi/jsonpointer v0.19.3/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.5 h1:gZr+CIYByUqjcgeLXnQu2gHYQC9o73G2XUeOFYEICuY=
github.com/go-openapi/jsonpointer v0.19.5/go.mod h1:Pl9vOtqEWErmShwVjC8pYs9cog34VGT37dQOVbmoatg=
github.com/go-openapi/jsonpointer v0.19.6/go.mod h1:osyAmYz/mB/C3I+WsTTSgw1ONzaLJoLCyoi6/zppojs=
github.com/go-openapi/jsonpointer v0.20.0 h1:ESKJdU9ASRfaPNOPRx12IUyA1vn3R9GiE3KYD14BXdQ=
github.com/go-openapi/jsonpointer v0.20.0/go.mod h1:6PGzBjjIIumbLYysB73Klnms1mwnU4G3YHOECG3CedA=
github.com/go-openapi/jsonreference v0.19.6 h1:UBIxjkht+AWIgYzCDSv2GN+E/togfwXUJFRTWhl2Jjs=
github.com/go-openapi/jsonreference v0.19.6/go.mod h1:diGHMEHg2IqXZGKxqyvWdfWU/aim5Dprw5bqpKkTvns=
github.com/go-openapi/jsonreference v0.20.0/go.mod h1:Ag74Ico3lPc+zR+qjn4XBUmXymS4zJbYVCZmcgkasdo=
github.com/go-openapi/jsonreference v0.20.2 h1:3sVjiK66+uXK/6oQ8xgcRKcFgQ5KXa2KvnJRumpMGbE=
github.com/go-openapi/jsonreference v0.20.2/go.mod h1:Bl1zwGIM8/wsvqjsOQLJ/SH+En5Ap4rVB5KVcIDZG2k=
github.com/go-openapi/spec v0.20.4 h1:O8hJrt0UMnhHcluhIdUgCLRWyM2x7QkBXRvOs7m+O1M=
github.com/go-openapi/spec v0.20.4/go.mod h1:faYFR1CvsJZ0mNsmsphTMSoRrNV3TEDoAM7FOEWeq8I=
github.com/go-openapi/spec v0.20.9 h1:xnlYNQAwKd2VQRRfwTEI0DcK+2cbuvI/0c7jx3gA8/8=
github.com/go-openapi/spec v0.20.9/go.mod h1:2OpW+JddWPrpXSCIX8eOx7lZ5iyuWj3RYR6VaaBKcWA=
github.com/go-openapi/swag v0.19.5/go.mod h1:POnQmlKehdgb5mhVOsnJFsivZCEZ/vjK9gh66Z9tfKk=
github.com/go-openapi/swag v0.19.15 h1:D2NRCBzS9/pEY3gP9Nl8aDqGUcPFrwG2p+CNFrLyrCM=
github.com/go-openapi/swag v0.19.15/go.mod h1:QYRuS/SOXUCsnplDa677K7+DxSOj6IPNl/eQntq43wQ=
github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogBU=
github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/golang-jwt/jwt v3.2.2+incompatible h1:IfV12K8xAKAnZqdXVzCZ+TOjboZ2keLg81eXfW3O+oY=
github.com/golang-jwt/jwt v3.2.2+incompatible/go.mod h1:8pz2t5EyA70fFQQSrl6XZXzqecmYZeUEB8OUGHkxJ+I=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/hashicorp/go-set v0.1.13 h1:k1B5goY3c7OKEzpK+gwAhJexxzAJwDN8kId8YvWrihA=
github.com/hashicorp/go-set v0.1.13/go.mod h1:0/D+R4MFUzJ6XmvjU7liXtznF1eQDxh84GJlhXw+lvo=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
@@ -29,12 +49,27 @@ github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=
github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/z8=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.1/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/labstack/echo/v4 v4.10.2 h1:n1jAhnq/elIFTHr1EYpiYtyKgx4RW9ccVgkqByZaN2M=
github.com/labstack/echo/v4 v4.10.2/go.mod h1:OEyqf2//K1DFdE57vw2DRgWY0M7s65IVQO2FzvI4J5k=
github.com/labstack/echo/v4 v4.11.0 h1:4Dmi59tmrnFzOchz4EXuGjJhUfcEkU28iDKsiZVOQgw=
github.com/labstack/echo/v4 v4.11.0/go.mod h1:YuYRTSM3CHs2ybfrL8Px48bO6BAnYIN4l8wSTMP6BDQ=
github.com/labstack/gommon v0.4.0 h1:y7cvthEAEbU0yHOf4axH8ZG2NH8knB9iNSoTO8dyIk8=
github.com/labstack/gommon v0.4.0/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/leodido/go-urn v1.2.4 h1:XlAE/cm/ms7TE/VMVoduSpNBoyc2dOxHs5MZSwAN63Q=
github.com/leodido/go-urn v1.2.4/go.mod h1:7ZrI8mTSeBSHl/UaRyKQW1qZeMgak41ANeCNaVckg+4=
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
github.com/lib/pq v1.10.9/go.mod h1:AlVN5x4E4T544tWzH6hKfbfQvm3HdbOxrmggDNAPY9o=
github.com/mailru/easyjson v0.0.0-20190614124828-94de47d64c63/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.0.0-20190626092158-b2ccc519800e/go.mod h1:C1wdFJiN94OJF2b5HbByQZoLdCWB1Yqtg26g4irojpc=
github.com/mailru/easyjson v0.7.6 h1:8yTIVnZgCoiM1TgqoeTl+LfU5Jg6/xL3QhGQnimLYnA=
github.com/mailru/easyjson v0.7.6/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
@@ -42,44 +77,105 @@ github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27k
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/mattn/go-isatty v0.0.19 h1:JITubQf0MOLdlGRuRq+jtsDlekdYPia9ZFsB8h/APPA=
github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mattn/go-sqlite3 v1.14.17 h1:mCRHCLDUBXgpKAqIKsaAaAsrAlbkeomtRFKXh2L6YIM=
github.com/mattn/go-sqlite3 v1.14.17/go.mod h1:2eHXhiwb8IkHr+BDWZGa96P6+rkvnG63S2DGjv9HUNg=
github.com/niemeyer/pretty v0.0.0-20200227124842-a10e7caefd8e/go.mod h1:zD1mROLANZcx1PVRCS0qkT7pwLkGfwJo4zjcN/Tysno=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/shoenig/test v0.6.4 h1:kVTaSd7WLz5WZ2IaoM0RSzRsUD+m8wRR+5qvntpn4LU=
github.com/shoenig/test v0.6.4/go.mod h1:byHiCGXqrVaflBLAMq/srcZIHynQPQgeyvkvXnjqq0k=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/swaggo/echo-swagger v1.4.0 h1:RCxLKySw1SceHLqnmc41pKyiIeE+OiD7NSI7FUOBlLo=
github.com/swaggo/echo-swagger v1.4.0/go.mod h1:Wh3VlwjZGZf/LH0s81tz916JokuPG7y/ZqaqnckYqoQ=
github.com/swaggo/files/v2 v2.0.0 h1:hmAt8Dkynw7Ssz46F6pn8ok6YmGZqHSVLZ+HQM7i0kw=
github.com/swaggo/files/v2 v2.0.0/go.mod h1:24kk2Y9NYEJ5lHuCra6iVwkMjIekMCaFq/0JQj66kyM=
github.com/swaggo/swag v1.16.1 h1:fTNRhKstPKxcnoKsytm4sahr8FaYzUcT7i1/3nd/fBg=
github.com/swaggo/swag v1.16.1/go.mod h1:9/LMvHycG3NFHfR6LwvikHv5iFvmPADQ359cKikGxto=
github.com/urfave/cli/v2 v2.3.0 h1:qph92Y649prgesehzOrQjdWyxFOp/QVM+6imKHad91M=
github.com/urfave/cli/v2 v2.3.0/go.mod h1:LJmUH05zAU44vOAcrfzZQKsZbVcdbOG8rtL3/XcUArI=
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
golang.org/x/crypto v0.6.0 h1:qfktjS5LUO+fFKeJXZ+ikTRijMmljikvG68fpMMruSc=
golang.org/x/crypto v0.6.0/go.mod h1:OFC/31mSvZgRz0V1QTNCzfAI1aIRzbiufJtkMIlEp58=
golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ=
golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE=
golang.org/x/crypto v0.11.0 h1:6Ewdq3tDic1mg5xRO4milcWCfMVQhI4NkqWWvqejpuA=
golang.org/x/crypto v0.11.0/go.mod h1:xgJhtzW8F9jGdVFWZESrid1U1bjeNy4zgy5cRr/CIio=
golang.org/x/net v0.0.0-20210421230115-4e50805a0758/go.mod h1:72T/g9IO56b78aLF+1Kcs5dz7/ng1VjMUvfKvpfy+jM=
golang.org/x/net v0.7.0 h1:rJrUqqhjsgNp7KqAIc25s9pZnjU7TUcSY7HcVZjdn1g=
golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.9.0 h1:aWJ/m6xSmxWBx+V0XRHTlrYrPG56jKsLdTFmsSsCzOM=
golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
golang.org/x/net v0.12.0 h1:cfawfvKITfUsFCeJIHJrbSxpeu/E81khclypR0GVT50=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210420072515-93ed5bcd2bfe/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU=
golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.10.0 h1:SqMFp9UcQJZa+pmYuAKjd9xq1f0j5rLcDIk0mj4qAsA=
golang.org/x/sys v0.10.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.7.0 h1:4BRB4x83lYWy72KwLD/qYDuTu7q9PjSagHvijDw7cLo=
golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/text v0.9.0 h1:2sjJmO8cDvYveuX97RDLsxlyUxLl+GHoLxBiRdHllBE=
golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
golang.org/x/text v0.11.0 h1:LAntKIrcmeSKERyiOh0XMV39LXS8IE9UL2yP7+f5ij4=
golang.org/x/text v0.11.0/go.mod h1:TvPlkZtksWOMsz7fbANvkp4WM8x/WCo/om8BMLbz+aE=
golang.org/x/time v0.3.0 h1:rg5rLMjNzMS1RkNLzCG38eapWhnYLFYXDXj2gOlr8j4=
golang.org/x/time v0.3.0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/tools v0.11.0 h1:EMCa6U9S2LtZXLAMoWiR/R8dAQFRqbAitmbJ2UKhoi8=
golang.org/x/tools v0.11.0/go.mod h1:anzJrxPjNtfgiYQYirP2CPGzGLxrH2u2QBhn6Bf3qY8=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.3/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20200615113413-eeeca48fe776/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/postgres v1.5.2 h1:ytTDxxEv+MplXOfFe3Lzm7SjG09fcdb3Z/c056DTBx0=
gorm.io/driver/postgres v1.5.2/go.mod h1:fmpX0m2I1PKuR7mKZiEluwrP3hbs+ps7JIGMUBpCgl8=
gorm.io/driver/sqlite v1.5.2 h1:TpQ+/dqCY4uCigCFyrfnrJnrW9zjpelWVoEVNy5qJkc=
gorm.io/driver/sqlite v1.5.2/go.mod h1:qxAuCol+2r6PannQDpOP1FP6ag3mKi4esLnB/jHed+4=
gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
gorm.io/gorm v1.25.2 h1:gs1o6Vsa+oVKG/a9ElL3XgyGfghFfkKA2SInQaCyMho=
gorm.io/gorm v1.25.2/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k=
sigs.k8s.io/yaml v1.3.0 h1:a2VclLzOGrwOHDiV8EfBGhvjHvP46CtW5j6POvhYGGo=
sigs.k8s.io/yaml v1.3.0/go.mod h1:GeOyir5tyXNByN85N/dRIT9es5UQNerPYEKK56eTBm8=

View File

@@ -4,278 +4,147 @@ import (
"astra-events/model"
"astra-events/pkg/utils"
"astra-events/services"
"errors"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/hashicorp/go-set"
"github.com/labstack/echo/v4"
"gorm.io/gorm"
"net/http"
"time"
"strconv"
)
// swagger:route POST /events/ Events CreateEvent
//
// responses:
// 200: Event
func (h *Handler) CreateEvent(c echo.Context) (err error) {
var person *model.PersonalInformationApi
newEvent := new(model.Event)
event := new(model.CreateEvent)
authorId, err := uuid.Parse(c.Request().Header.Get("X-User-Id"))
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
if err != nil {
return c.JSON(http.StatusUnauthorized, &utils.Error{
Type: "internal_error",
Message: "User in not authorized",
Code: "0002",
})
}
err = c.Bind(&event)
err = h.Validator.Struct(event)
authorId, err := strconv.Atoi(c.Request().Header.Get("UserId"))
orgId, err := strconv.Atoi(c.Request().Header.Get("UserOrganizationId"))
if err != nil {
var validationErrors validator.ValidationErrors
errors.As(err, &validationErrors)
Fields := make(map[string]string)
for _, validationError := range validationErrors {
Fields[validationError.Field()] = validationError.Tag()
}
return c.JSON(http.StatusBadRequest, &utils.ValidationError{
Type: "validation_error",
Message: err.Error(),
Code: "0001",
Fields: Fields,
})
return c.NoContent(http.StatusUnauthorized)
}
if event.Person.ID.ID() != 0 {
person, err = services.GetPerson(event.Person.ID, c.Request().Header, c)
if err != nil || person.ID != event.Person.ID {
return c.JSON(http.StatusBadGateway, &utils.Error{
Type: "internal_error",
Message: "Can't send request to persons service",
Code: "0003",
})
}
} else {
newPerson := model.PersonalInformationApi{
FirstName: event.Person.FirstName,
LastName: event.Person.LastName,
Patronymic: event.Person.Patronymic,
Contacts: []model.Contact{
{
Category: "PHONE",
Value: event.Person.Phone,
},
},
}
person, err = services.CreatePerson(newPerson, c.Request().Header, c)
if err != nil || person.ID.ID() == 0 {
return c.JSON(http.StatusBadGateway, &utils.Error{
Type: "internal_error",
Message: "Can't send request to persons service",
Code: "0004",
})
}
if err = c.Bind(event); err != nil {
return c.NoContent(http.StatusBadRequest)
}
usersMap, err := services.GetUsers([]uuid.UUID{event.MedicId, authorId}, c.Request().Header, c)
usersMap, err := services.GetUsers([]uint{event.EmployeeId, uint(authorId)}, c.Request().Header)
if err != nil || len(usersMap) == 0 {
return c.JSON(http.StatusBadGateway, &utils.Error{
Type: "internal_error",
Message: "Can't send request to users service",
Code: "0005",
})
if err != nil {
return c.HTML(http.StatusBadGateway, err.Error())
}
medic := usersMap[event.MedicId]
author := usersMap[authorId]
employee := usersMap[event.EmployeeId]
author := usersMap[uint(authorId)]
newEvent, err := utils.TypeConverter[model.Event](event)
newEvent.OrgId = orgId
newEvent.MedicId = event.MedicId
newEvent.AuthorId = authorId
newEvent.PersonId = person.ID
newEvent.Person = *person
newEvent.Start = event.Start
newEvent.End = event.End
newEvent.AuthorId = uint(authorId)
newEvent.EmployeeId = event.EmployeeId
newEvent.MemberId = event.MemberId
newEvent.MedicalCardId = event.MedicalCardId
newEvent.Status = event.Status
newEvent.Services = event.Services
newEvent.OrgId = uint(orgId)
tx := h.DB.Create(&newEvent)
if tx.RowsAffected == 0 {
return c.JSON(http.StatusBadGateway, &utils.Error{
Type: "internal_error",
Message: "Database error",
Code: "0006",
})
return c.NoContent(http.StatusBadGateway)
}
newEvent.Author = author
newEvent.Medic = medic
newEvent.Employee = employee
return c.JSON(http.StatusCreated, newEvent)
}
// swagger:route GET /events/ Events GetEvents
//
// responses:
// 200: []Event
// ListEvents godoc
// @Summary List events
// @Description get events
// @Tags events
// @Accept json
// @Produce json
// @Success 200 {array} model.Event
// @Router / [get]
func (h *Handler) GetEvents(c echo.Context) (err error) {
var events []model.Event
query := c.Request().URL.Query()
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
_, _, filterMap := utils.GetFilterParams(query)
orgId, err := strconv.Atoi(c.Request().Header.Get("UserOrganizationId"))
if err != nil {
return c.JSON(http.StatusUnauthorized, &utils.Error{
Type: "internal_error",
Message: "User in not authorized",
Code: "0002",
})
return c.NoContent(http.StatusUnauthorized)
}
tx := h.DB.Where("org_id = ?", orgId)
if query.Has("start") && query.Has("end") {
tx = tx.Where("start >= ?", query.Get("start")).Where("\"end\" <= ?", query.Get("end"))
if len(filterMap) > 0 {
tx = tx.Where("start >= ?", filterMap["start"]).Where("\"end\" <= ?", filterMap["end"])
}
return h.queryEvents(tx, c)
}
tx.Find(&events)
// swagger:route GET /events/today Events GetEventsToday
//
// responses:
// 200: []Event
ids := set.New[uint](len(events) * 2)
func (h *Handler) GetEventsToday(c echo.Context) (err error) {
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
for _, event := range events {
ids.Insert(event.EmployeeId)
ids.Insert(event.AuthorId)
}
usersMap, err := services.GetUsers(ids.Slice(), c.Request().Header)
if err != nil {
return c.JSON(http.StatusUnauthorized, &utils.Error{
Type: "internal_error",
Message: "User in not authorized",
Code: "0002",
})
return c.HTML(http.StatusBadGateway, err.Error())
}
tx := h.DB.Where("org_id = ?", orgId)
for i, event := range events {
events[i].Author = usersMap[event.AuthorId]
events[i].Employee = usersMap[event.EmployeeId]
}
currentDate := time.Now().Format(time.DateOnly)
nextDate := time.Now().Add(24 * time.Hour).Format(time.DateOnly)
tx = tx.Where("start >= ?", currentDate).Where("\"end\" < ?", nextDate)
return h.queryEvents(tx, c)
return c.JSON(http.StatusOK, events)
}
// swagger:route GET /events/{uuid} Events GetEvent
//
// responses:
// 200: Event
func (h *Handler) GetEvent(c echo.Context) error {
var event model.Event
id := c.Param("id")
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
orgId, err := strconv.Atoi(c.Request().Header.Get("UserOrganizationId"))
if err != nil {
return c.JSON(http.StatusUnauthorized, &utils.Error{
Type: "internal_error",
Message: "User in not authorized",
Code: "0002",
})
return c.NoContent(http.StatusUnauthorized)
}
tx := h.DB.Where("org_id = ?", orgId).First(&event, id)
if tx.RowsAffected == 0 {
return c.JSON(http.StatusNotFound, &utils.Error{
Type: "not_found_error",
Message: "Event not found",
Code: "0007",
})
return c.NoContent(http.StatusNotFound)
}
usersMap, err := services.GetUsers([]uuid.UUID{event.MedicId, event.AuthorId}, c.Request().Header, c)
usersMap, err := services.GetUsers([]uint{event.EmployeeId, event.AuthorId}, c.Request().Header)
if err != nil {
return c.JSON(http.StatusBadGateway, &utils.Error{
Type: "internal_error",
Message: "Can't send request to persons or users service",
Code: "0005",
})
return c.HTML(http.StatusBadGateway, err.Error())
}
medic := usersMap[event.MedicId]
employee := usersMap[event.EmployeeId]
author := usersMap[event.AuthorId]
event.Medic = medic
event.Employee = employee
event.Author = author
return c.JSON(http.StatusOK, event)
}
// swagger:route DELETE /events/{uuid} Events DeleteEvent
//
// responses:
// 200
func (h *Handler) DeleteEvent(c echo.Context) error {
id := c.Param("id")
orgId, err := uuid.Parse(c.Request().Header.Get("X-User-Organization-Id"))
orgId, err := strconv.Atoi(c.Request().Header.Get("UserOrganizationId"))
if err != nil {
return c.JSON(http.StatusUnauthorized, &utils.Error{
Type: "internal_error",
Message: "User in not authorized",
Code: "0002",
})
return c.NoContent(http.StatusUnauthorized)
}
tx := h.DB.Where("org_id = ?", orgId).Delete(&model.Event{}, id)
if tx.RowsAffected == 0 {
return c.JSON(http.StatusNotFound, &utils.Error{
Type: "not_found_error",
Message: "Event not found",
Code: "0007",
})
return c.NoContent(http.StatusNotFound)
}
return c.NoContent(http.StatusOK)
}
func (h *Handler) queryEvents(tx *gorm.DB, c echo.Context) error {
var events []model.Event
tx.Find(&events)
userIds := set.New[uuid.UUID](len(events) * 2)
personIds := set.New[uuid.UUID](len(events))
for _, event := range events {
userIds.Insert(event.MedicId)
userIds.Insert(event.AuthorId)
personIds.Insert(event.PersonId)
}
usersMap, err := services.GetUsers(userIds.Slice(), c.Request().Header, c)
personsMap, err := services.GetPersons(personIds.Slice(), c.Request().Header, c)
if err != nil {
return c.JSON(http.StatusBadGateway, &utils.Error{
Type: "internal_error",
Message: "Can't send request to persons or users service",
Code: "0005",
})
}
for i, event := range events {
events[i].Author = usersMap[event.AuthorId]
events[i].Medic = usersMap[event.MedicId]
events[i].Person = personsMap[event.PersonId]
}
return c.JSON(http.StatusOK, events)
}

View File

@@ -1,11 +1,7 @@
package handler
import (
"github.com/go-playground/validator/v10"
"gorm.io/gorm"
)
import "gorm.io/gorm"
type Handler struct {
DB *gorm.DB
Validator *validator.Validate
DB *gorm.DB
}

31
main.go
View File

@@ -2,22 +2,30 @@ package main
import (
"astra-events/config"
_ "astra-events/docs"
"astra-events/handler"
"astra-events/model"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
log2 "github.com/labstack/gommon/log"
"github.com/swaggo/echo-swagger"
"gorm.io/driver/postgres"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"reflect"
"strings"
"time"
)
// @title Astra Events
// @version 1.0
// @description This is an astra-events service.
// @contact.name Kirill Andrusyak
// @contact.email andrusyakka@dopcore.com
// @host api.astra.dev.dop
// @BasePath /events
func main() {
newLogger := logger.New(
log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
@@ -42,27 +50,16 @@ func main() {
db.AutoMigrate(&model.Event{})
validate := validator.New()
validate.RegisterTagNameFunc(func(fld reflect.StructField) string {
name := strings.SplitN(fld.Tag.Get("json"), ",", 2)[0]
if name == "-" {
return ""
}
return name
})
var h = &handler.Handler{DB: db, Validator: validate}
var h = &handler.Handler{DB: db}
e.POST("/", h.CreateEvent)
e.GET("/:id", h.GetEvent)
e.GET("/", h.GetEvents)
e.GET("/today", h.GetEventsToday)
e.DELETE("/", h.DeleteEvent)
e.GET("/health", handler.Health)
e.Debug = true
e.Logger.SetLevel(log2.DEBUG)
e.GET("/swagger/*", echoSwagger.WrapHandler)
e.Logger.Fatal(e.Start(":" + config.Env.Server.Port))
}

View File

@@ -1,47 +1,34 @@
package model
import (
"github.com/google/uuid"
"github.com/lib/pq"
"gorm.io/gorm"
"time"
)
// swagger:model Event
type Event struct {
gorm.Model `json:"meta"`
ID uuid.UUID `json:"id" gorm:"type:uuid;default:gen_random_uuid();primarykey"`
Start time.Time `json:"start"`
End time.Time `json:"end"`
Author User `json:"author" gorm:"-"`
AuthorId uuid.UUID `json:"-"`
Medic User `json:"medic" gorm:"-"`
MedicId uuid.UUID `json:"-"`
Person PersonalInformationApi `json:"person" gorm:"-"`
PersonId uuid.UUID `json:"-"`
OrgId uuid.UUID `json:"-"`
gorm.Model
Start time.Time `json:"start"`
End time.Time `json:"end"`
Author User `json:"author" gorm:"-"`
AuthorId uint `json:"-"`
Employee User `json:"employee" gorm:"-"`
EmployeeId uint `json:"-"`
Member PD `json:"member" gorm:"-"`
MemberId uint `json:"-"`
Status string `json:"status"`
MedicalCard Card `json:"medicalCard" gorm:"-"`
MedicalCardId uint `json:"-"`
Services pq.Int32Array `json:"services" gorm:"type:integer[]"`
OrgId uint `json:"-"`
}
// swagger:model CreateEvent
type CreateEvent struct {
// The event start time
// example: 2023-08-02T19:21:23.617Z
// required: true
Start time.Time `json:"start" validate:"required,gt"`
// The event end time
// example: 2023-08-02T19:21:23.617Z
// required: true
End time.Time `json:"end" validate:"required,gtfield=Start"`
// The person information
// required: true
Person *PersonalInformation `json:"person" validate:"required,dive"`
// The UUID of a medic
// example: 6204037c-30e6-408b-8aaa-dd8219860b4b
// required: true
MedicId uuid.UUID `json:"medic_id" validate:"required"`
}
// swagger:parameters CreateEvent
type CreateEventArg struct {
//in: body
CreateEvent *CreateEvent
Start time.Time `json:"start"`
End time.Time `json:"end"`
EmployeeId uint `json:"employee_id"`
MemberId uint `json:"member_id"`
Status string `json:"status"`
MedicalCardId uint `json:"medical_card_id"`
Services []int32 `json:"services"`
}

7
model/pd.go Normal file
View File

@@ -0,0 +1,7 @@
package model
import "gorm.io/gorm"
type PD struct {
gorm.Model
}

View File

@@ -1,36 +0,0 @@
package model
import "github.com/google/uuid"
// swagger:model PersonalInformation
type PersonalInformation struct {
// The UUID of a person
// example: 6204037c-30e6-408b-8aaa-dd8219860b4b
ID uuid.UUID `json:"id,omitempty" validate:"required_without_all=FirstName LastName Patronymic Phone"`
// The first name of a person
// example: Иван
FirstName string `json:"first_name,omitempty" validate:"required_without=ID"`
// The last name of a person
// example: Иванов
LastName string `json:"last_name,omitempty" validate:"required_without=ID"`
// The patronymic of a person
// example: Иванович
Patronymic string `json:"patronymic,omitempty" validate:"required_without=ID"`
// The first name of a person
// example: 79206321370
Phone string `json:"phone,omitempty" validate:"required_without=ID"`
}
type PersonalInformationApi struct {
ID uuid.UUID `json:"id,omitempty"`
FirstName string `json:"first_name,omitempty"`
LastName string `json:"last_name,omitempty"`
Patronymic string `json:"patronymic,omitempty"`
BirthDate string `json:"birth_date,omitempty"`
Contacts []Contact `json:"contacts"`
}
type Contact struct {
Category string `json:"category"`
Value string `json:"value"`
}

View File

@@ -1,13 +1,9 @@
package model
import (
"github.com/google/uuid"
"gorm.io/gorm"
)
import "gorm.io/gorm"
type (
User struct {
ID uuid.UUID
gorm.Model
UserName string `json:"user_name" gorm:"unique"`
Password string `json:"password,omitempty"`

View File

@@ -3,16 +3,15 @@ package api
import (
"bytes"
"encoding/json"
"github.com/labstack/echo/v4"
"io"
"net/http"
)
func PostRequest(url string, model any, header http.Header, c echo.Context) ([]byte, error, *http.Response) {
func PostRequest(url string, model any, header http.Header) ([]byte, error) {
uJson, err := json.Marshal(model)
if err != nil {
return nil, err, nil
return nil, err
}
client := http.Client{}
@@ -20,28 +19,22 @@ func PostRequest(url string, model any, header http.Header, c echo.Context) ([]b
req.Header = header
req.Header.Set("Content-Type", "application/json")
c.Logger().Debug("Request---")
c.Logger().Debug(string(uJson))
resp, err := client.Do(req)
if err != nil {
return nil, err, nil
return nil, err
}
data, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err, nil
return nil, err
}
c.Logger().Debug("Response---")
c.Logger().Debug(string(data))
return data, nil, resp
return data, nil
}
func GetRequest(url string, header http.Header, c echo.Context, args ...string) ([]byte, error, *http.Response) {
func GetRequest(url string, header http.Header, args ...string) ([]byte, error, *http.Response) {
client := http.Client{}
req, err := http.NewRequest("GET", url, nil)
if len(args) > 0 {
@@ -49,10 +42,6 @@ func GetRequest(url string, header http.Header, c echo.Context, args ...string)
}
req.Header = header
req.Header.Set("Content-Type", "application/json")
c.Logger().Debug("Request---")
c.Logger().Debug(req.URL.String())
resp, err := client.Do(req)
if err != nil {
@@ -65,9 +54,6 @@ func GetRequest(url string, header http.Header, c echo.Context, args ...string)
return nil, err, nil
}
c.Logger().Debug("Response---")
c.Logger().Debug(string(data))
return data, nil, resp
}

View File

@@ -1,16 +0,0 @@
package utils
import "encoding/json"
func TypeConverter[R any](data any) (*R, error) {
var result R
b, err := json.Marshal(&data)
if err != nil {
return nil, err
}
err = json.Unmarshal(b, &result)
if err != nil {
return nil, err
}
return &result, err
}

View File

@@ -1,14 +0,0 @@
package utils
type Error struct {
Type string `json:"type"`
Message string `json:"message"`
Code string `json:"code"`
}
type ValidationError struct {
Type string `json:"type"`
Message string `json:"message"`
Code string `json:"code"`
Fields map[string]string `json:"fields"`
}

View File

@@ -1,82 +0,0 @@
package services
import (
"astra-events/config"
"astra-events/model"
"astra-events/pkg/api"
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"net/http"
)
func GetPerson(id uuid.UUID, header http.Header, c echo.Context) (*model.PersonalInformationApi, error) {
data, err, _ := api.GetRequest(
config.Env.MSHosts.PersonsHost+"/persons/"+id.String(),
header, c)
if err != nil {
return nil, errors.New("error Request to Personal Information Service")
}
pi := new(model.PersonalInformationApi)
err = json.Unmarshal(data, &pi)
if err != nil {
return nil, errors.New("bad Response from Personal Information Service")
}
return pi, nil
}
func CreatePerson(person model.PersonalInformationApi, header http.Header, c echo.Context) (*model.PersonalInformationApi, error) {
data, err, _ := api.PostRequest(
config.Env.MSHosts.PersonsHost+"/persons/",
person,
header,
c,
)
if err != nil {
return nil, errors.New("error Request to Personal Information Service")
}
pi := new(model.PersonalInformationApi)
err = json.Unmarshal(data, &pi)
if err != nil {
return nil, errors.New("bad Response from Personal Information Service")
}
return pi, nil
}
func GetPersons(ids []uuid.UUID, header http.Header, c echo.Context) (map[uuid.UUID]model.PersonalInformationApi, error) {
var persons []model.PersonalInformationApi
personsMap := make(map[uuid.UUID]model.PersonalInformationApi)
data, err, req := api.PostRequest(
config.Env.MSHosts.PersonsHost+"/persons/batch/",
&map[string][]uuid.UUID{
"ids": ids,
},
header, c)
if err != nil || req.StatusCode != 200 {
return nil, err
}
err = json.Unmarshal(data, &persons)
if err != nil {
return nil, err
}
for _, person := range persons {
personsMap[person.ID] = person
}
return personsMap, nil
}

View File

@@ -6,19 +6,17 @@ import (
"astra-events/pkg/api"
"encoding/json"
"errors"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"net/http"
"net/url"
"strconv"
)
func GetUser(ID uint, header http.Header, c echo.Context) (*model.User, error) {
func GetUser(ID uint, header http.Header) (*model.User, error) {
id := strconv.Itoa(int(ID))
data, err, _ := api.GetRequest(
config.Env.MSHosts.UsersHost+"/"+id,
header, c)
header)
if err != nil {
return nil, errors.New("error Request to Users Service")
@@ -35,9 +33,9 @@ func GetUser(ID uint, header http.Header, c echo.Context) (*model.User, error) {
return u, nil
}
func GetUsers(ids []uuid.UUID, header http.Header, c echo.Context) (map[uuid.UUID]model.User, error) {
func GetUsers(ids []uint, header http.Header) (map[uint]model.User, error) {
var users []model.User
var usersMap = make(map[uuid.UUID]model.User)
var usersMap = make(map[uint]model.User)
Url, err := url.Parse(config.Env.MSHosts.UsersHost)
if err != nil {
return nil, err
@@ -49,7 +47,7 @@ func GetUsers(ids []uuid.UUID, header http.Header, c echo.Context) (map[uuid.UUI
data, err, req := api.GetRequest(
Url.String(),
header, c)
header)
if err != nil || req.StatusCode != 200 {
return nil, err

View File

@@ -1,335 +0,0 @@
{
"swagger": "2.0",
"paths": {
"/events/": {
"get": {
"tags": [
"Events"
],
"operationId": "GetEvents",
"responses": {
"200": {
"description": "Event",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Event"
}
}
}
}
},
"post": {
"tags": [
"Events"
],
"operationId": "CreateEvent",
"parameters": [
{
"name": "CreateEvent",
"in": "body",
"schema": {
"$ref": "#/definitions/CreateEvent"
}
}
],
"responses": {
"200": {
"description": "Event",
"schema": {
"$ref": "#/definitions/Event"
}
}
}
}
},
"/events/today": {
"get": {
"tags": [
"Events"
],
"operationId": "GetEvents",
"responses": {
"200": {
"description": "Event",
"schema": {
"type": "array",
"items": {
"$ref": "#/definitions/Event"
}
}
}
}
}
},
"/events/{uuid}": {
"get": {
"tags": [
"Events"
],
"operationId": "GetEvent",
"responses": {
"200": {
"description": "Event",
"schema": {
"$ref": "#/definitions/Event"
}
}
}
},
"delete": {
"tags": [
"Events"
],
"operationId": "DeleteEvent",
"responses": {}
}
}
},
"definitions": {
"Contact": {
"type": "object",
"properties": {
"category": {
"type": "string",
"x-go-name": "Category"
},
"value": {
"type": "string",
"x-go-name": "Value"
}
},
"x-go-package": "astra-events/model"
},
"CreateEvent": {
"type": "object",
"required": [
"start",
"end",
"person",
"medic_id"
],
"properties": {
"end": {
"description": "The event end time",
"type": "string",
"format": "date-time",
"x-go-name": "End",
"example": "2023-08-02T19:21:23.617Z"
},
"medic_id": {
"description": "The UUID of a medic",
"type": "string",
"format": "uuid",
"x-go-name": "MedicId",
"example": "6204037c-30e6-408b-8aaa-dd8219860b4b"
},
"person": {
"$ref": "#/definitions/PersonalInformation"
},
"start": {
"description": "The event start time",
"type": "string",
"format": "date-time",
"x-go-name": "Start",
"example": "2023-08-02T19:21:23.617Z"
}
},
"x-go-package": "astra-events/model"
},
"DeletedAt": {
"$ref": "#/definitions/NullTime"
},
"Event": {
"type": "object",
"properties": {
"CreatedAt": {
"type": "string",
"format": "date-time"
},
"DeletedAt": {
"$ref": "#/definitions/DeletedAt"
},
"ID": {
"type": "integer",
"format": "uint64"
},
"UpdatedAt": {
"type": "string",
"format": "date-time"
},
"author": {
"$ref": "#/definitions/User"
},
"end": {
"type": "string",
"format": "date-time",
"x-go-name": "End"
},
"id": {
"type": "string",
"format": "uuid",
"x-go-name": "ID"
},
"medic": {
"$ref": "#/definitions/User"
},
"person": {
"$ref": "#/definitions/PersonalInformationApi"
},
"start": {
"type": "string",
"format": "date-time",
"x-go-name": "Start"
}
},
"x-go-package": "astra-events/model"
},
"Model": {
"description": "type User struct {\ngorm.Model\n}",
"type": "object",
"title": "Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt\nIt may be embedded into your model or you may build your own model without it",
"properties": {
"CreatedAt": {
"type": "string",
"format": "date-time"
},
"DeletedAt": {
"$ref": "#/definitions/DeletedAt"
},
"ID": {
"type": "integer",
"format": "uint64"
},
"UpdatedAt": {
"type": "string",
"format": "date-time"
}
},
"x-go-package": "gorm.io/gorm"
},
"NullTime": {
"description": "NullTime implements the Scanner interface so\nit can be used as a scan destination, similar to NullString.",
"type": "object",
"title": "NullTime represents a time.Time that may be null.",
"properties": {
"Time": {
"type": "string",
"format": "date-time"
},
"Valid": {
"type": "boolean"
}
},
"x-go-package": "database/sql"
},
"PersonalInformation": {
"type": "object",
"properties": {
"first_name": {
"description": "The first name of a person",
"type": "string",
"x-go-name": "FirstName",
"example": "Иван"
},
"id": {
"description": "The UUID of a person",
"type": "string",
"format": "uuid",
"x-go-name": "ID",
"example": "6204037c-30e6-408b-8aaa-dd8219860b4b"
},
"last_name": {
"description": "The last name of a person",
"type": "string",
"x-go-name": "LastName",
"example": "Иванов"
},
"patronymic": {
"description": "The patronymic of a person",
"type": "string",
"x-go-name": "Patronymic",
"example": "Иванович"
},
"phone": {
"description": "The first name of a person",
"type": "string",
"x-go-name": "Phone",
"example": "79206321370"
}
},
"x-go-package": "astra-events/model"
},
"PersonalInformationApi": {
"type": "object",
"properties": {
"birth_date": {
"type": "string",
"x-go-name": "BirthDate"
},
"contacts": {
"type": "array",
"items": {
"$ref": "#/definitions/Contact"
},
"x-go-name": "Contacts"
},
"first_name": {
"type": "string",
"x-go-name": "FirstName"
},
"id": {
"type": "string",
"format": "uuid",
"x-go-name": "ID"
},
"last_name": {
"type": "string",
"x-go-name": "LastName"
},
"patronymic": {
"type": "string",
"x-go-name": "Patronymic"
}
},
"x-go-package": "astra-events/model"
},
"User": {
"type": "object",
"properties": {
"CreatedAt": {
"type": "string",
"format": "date-time"
},
"DeletedAt": {
"$ref": "#/definitions/DeletedAt"
},
"ID": {
"type": "string",
"format": "uuid"
},
"UpdatedAt": {
"type": "string",
"format": "date-time"
},
"first_name": {
"type": "string",
"x-go-name": "FirstName"
},
"last_name": {
"type": "string",
"x-go-name": "LastName"
},
"password": {
"type": "string",
"x-go-name": "Password"
},
"user_name": {
"type": "string",
"x-go-name": "UserName"
}
},
"x-go-package": "astra-events/model"
}
}
}