diff --git a/account.go b/account.go new file mode 100644 index 0000000..6fb043e --- /dev/null +++ b/account.go @@ -0,0 +1,54 @@ +package esaccount + +import ( + "database/sql/driver" + "encoding/json" + "fmt" +) + +type ConfidentialType string +type ConfidentialSettings map[string]ConfidentialType +type Int64Slice []int64 + + + +const ( + All ConfidentialType = "ALL" + Friends ConfidentialType = "FRIENDS" + Nobody ConfidentialType = "NOBODY" +) + +type Account struct { + Id int64 `json:"id" gorm:"unique;primaryKey"` + Description string `json:"description"` + DateOfBirth string `json:"date_of_birth"` + Follows Int64Slice `json:"follows" gorm:"type:text"` + Followers Int64Slice `json:"followers" gorm:"type:text"` + Confidentials ConfidentialSettings `json:"confidentials" gorm:"type:text"` +} + +// Implementing the driver.Valuer interface for Int64Slice +func (s Int64Slice) Value() (driver.Value, error) { + return json.Marshal(s) +} + +// Implementing the sql.Scanner interface for Int64Slice +func (s *Int64Slice) Scan(value interface{}) error { + if err := json.Unmarshal(value.([]byte), s); err != nil { + return fmt.Errorf("failed to unmarshal Int64Slice: %w", err) + } + return nil +} + +// Implementing the driver.Valuer interface for ConfidentialSettings +func (c ConfidentialSettings) Value() (driver.Value, error) { + return json.Marshal(c) +} + +// Implementing the sql.Scanner interface for ConfidentialSettings +func (c *ConfidentialSettings) Scan(value interface{}) error { + if err := json.Unmarshal(value.([]byte), c); err != nil { + return fmt.Errorf("failed to unmarshal ConfidentialSettings: %w", err) + } + return nil +} \ No newline at end of file diff --git a/docs/docs.go b/docs/docs.go new file mode 100644 index 0000000..1381440 --- /dev/null +++ b/docs/docs.go @@ -0,0 +1,527 @@ +// Package docs 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": "API Support", + "email": "red.tea.dev@gmail.com" + }, + "version": "{{.Version}}" + }, + "host": "{{.Host}}", + "basePath": "{{.BasePath}}", + "paths": { + "/api/account": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Get account information", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.accountResponce" + } + } + } + }, + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Set account information", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "account info", + "name": "account", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handler.accountInput" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.accountInput" + } + } + } + } + }, + "/api/account/:id": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Get account information by id", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "integer", + "description": "account id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.accountResponce" + } + } + } + } + }, + "/api/account/picture": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Get account information", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "file", + "description": "Picture", + "name": "file", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/account/{id}": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Set account information by id", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "account info", + "name": "account", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handler.accountResponce" + } + }, + { + "type": "integer", + "description": "account id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.accountResponce" + } + } + } + }, + "delete": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Delete account by id", + "parameters": [ + { + "type": "integer", + "description": "id of user", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/confidential/": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Get all confidential settings", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + }, + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Set confidential settings", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "Confidencial settings", + "name": "confidencial", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/esaccount.AuthorizedUser" + } + } + } + } + }, + "/api/confidential/{id}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Get all confidential settings by id", + "parameters": [ + { + "type": "integer", + "description": "id of user", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + }, + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Set confidential settings by id", + "parameters": [ + { + "type": "integer", + "description": "id of user", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "Confidencial settings", + "name": "confidencial", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/esaccount.AuthorizedUser" + } + } + } + } + }, + "/api/following/follow/{id}": { + "put": { + "produces": [ + "application/json" + ], + "tags": [ + "following" + ], + "summary": "Follow to user", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/following/unfollow/{id}": { + "put": { + "produces": [ + "application/json" + ], + "tags": [ + "following" + ], + "summary": "Unfollow user", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/public/{id}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Get all public data of users", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header" + }, + { + "type": "integer", + "description": "user id", + "name": "id", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + } + }, + "definitions": { + "esaccount.AuthorizedUser": { + "type": "object", + "properties": { + "access": { + "type": "string" + }, + "email": { + "type": "string" + }, + "first_name": { + "type": "string" + }, + "google_id": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "last_name": { + "type": "string" + }, + "picture_url": { + "type": "string" + }, + "telegram_id": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "handler.accountInput": { + "type": "object", + "properties": { + "date_of_birth": { + "type": "string" + }, + "description": { + "type": "string" + } + } + }, + "handler.accountResponce": { + "type": "object", + "properties": { + "date_of_birth": { + "type": "string" + }, + "description": { + "type": "string" + }, + "followers": { + "type": "array", + "items": { + "type": "integer" + } + }, + "follows": { + "type": "array", + "items": { + "type": "integer" + } + } + } + } + } +}` + +// SwaggerInfo holds exported Swagger Info so clients can modify it +var SwaggerInfo = &swag.Spec{ + Version: "1.0", + Host: "account.easystartup.su", + BasePath: "/api", + Schemes: []string{}, + Title: "EasyStartup Account", + Description: "Account service for EasyStartup.", + InfoInstanceName: "swagger", + SwaggerTemplate: docTemplate, + LeftDelim: "{{", + RightDelim: "}}", +} + +func init() { + swag.Register(SwaggerInfo.InstanceName(), SwaggerInfo) +} diff --git a/docs/swagger.json b/docs/swagger.json new file mode 100644 index 0000000..4a28374 --- /dev/null +++ b/docs/swagger.json @@ -0,0 +1,503 @@ +{ + "swagger": "2.0", + "info": { + "description": "Account service for EasyStartup.", + "title": "EasyStartup Account", + "contact": { + "name": "API Support", + "email": "red.tea.dev@gmail.com" + }, + "version": "1.0" + }, + "host": "account.easystartup.su", + "basePath": "/api", + "paths": { + "/api/account": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Get account information", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.accountResponce" + } + } + } + }, + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Set account information", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "account info", + "name": "account", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handler.accountInput" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.accountInput" + } + } + } + } + }, + "/api/account/:id": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Get account information by id", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "integer", + "description": "account id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.accountResponce" + } + } + } + } + }, + "/api/account/picture": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Get account information", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "type": "file", + "description": "Picture", + "name": "file", + "in": "formData", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/account/{id}": { + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Set account information by id", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "account info", + "name": "account", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/handler.accountResponce" + } + }, + { + "type": "integer", + "description": "account id", + "name": "id", + "in": "path", + "required": true + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/handler.accountResponce" + } + } + } + }, + "delete": { + "produces": [ + "application/json" + ], + "tags": [ + "account" + ], + "summary": "Delete account by id", + "parameters": [ + { + "type": "integer", + "description": "id of user", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/confidential/": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Get all confidential settings", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + }, + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Set confidential settings", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "Confidencial settings", + "name": "confidencial", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/esaccount.AuthorizedUser" + } + } + } + } + }, + "/api/confidential/{id}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Get all confidential settings by id", + "parameters": [ + { + "type": "integer", + "description": "id of user", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + }, + "post": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Set confidential settings by id", + "parameters": [ + { + "type": "integer", + "description": "id of user", + "name": "id", + "in": "path", + "required": true + }, + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + }, + { + "description": "Confidencial settings", + "name": "confidencial", + "in": "body", + "required": true, + "schema": { + "type": "string" + } + } + ], + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/esaccount.AuthorizedUser" + } + } + } + } + }, + "/api/following/follow/{id}": { + "put": { + "produces": [ + "application/json" + ], + "tags": [ + "following" + ], + "summary": "Follow to user", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/following/unfollow/{id}": { + "put": { + "produces": [ + "application/json" + ], + "tags": [ + "following" + ], + "summary": "Unfollow user", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + }, + "/api/public/{id}": { + "get": { + "produces": [ + "application/json" + ], + "tags": [ + "confidential" + ], + "summary": "Get all public data of users", + "parameters": [ + { + "type": "string", + "description": "JWT Bearer token (authorization)", + "name": "Authorization", + "in": "header" + }, + { + "type": "integer", + "description": "user id", + "name": "id", + "in": "query", + "required": true + } + ], + "responses": { + "200": { + "description": "OK" + } + } + } + } + }, + "definitions": { + "esaccount.AuthorizedUser": { + "type": "object", + "properties": { + "access": { + "type": "string" + }, + "email": { + "type": "string" + }, + "first_name": { + "type": "string" + }, + "google_id": { + "type": "string" + }, + "id": { + "type": "integer" + }, + "last_name": { + "type": "string" + }, + "picture_url": { + "type": "string" + }, + "telegram_id": { + "type": "string" + }, + "username": { + "type": "string" + } + } + }, + "handler.accountInput": { + "type": "object", + "properties": { + "date_of_birth": { + "type": "string" + }, + "description": { + "type": "string" + } + } + }, + "handler.accountResponce": { + "type": "object", + "properties": { + "date_of_birth": { + "type": "string" + }, + "description": { + "type": "string" + }, + "followers": { + "type": "array", + "items": { + "type": "integer" + } + }, + "follows": { + "type": "array", + "items": { + "type": "integer" + } + } + } + } + } +} \ No newline at end of file diff --git a/docs/swagger.yaml b/docs/swagger.yaml new file mode 100644 index 0000000..bc77d60 --- /dev/null +++ b/docs/swagger.yaml @@ -0,0 +1,329 @@ +basePath: /api +definitions: + esaccount.AuthorizedUser: + properties: + access: + type: string + email: + type: string + first_name: + type: string + google_id: + type: string + id: + type: integer + last_name: + type: string + picture_url: + type: string + telegram_id: + type: string + username: + type: string + type: object + handler.accountInput: + properties: + date_of_birth: + type: string + description: + type: string + type: object + handler.accountResponce: + properties: + date_of_birth: + type: string + description: + type: string + followers: + items: + type: integer + type: array + follows: + items: + type: integer + type: array + type: object +host: account.easystartup.su +info: + contact: + email: red.tea.dev@gmail.com + name: API Support + description: Account service for EasyStartup. + title: EasyStartup Account + version: "1.0" +paths: + /api/account: + get: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/handler.accountResponce' + summary: Get account information + tags: + - account + post: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + - description: account info + in: body + name: account + required: true + schema: + $ref: '#/definitions/handler.accountInput' + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/handler.accountInput' + summary: Set account information + tags: + - account + /api/account/:id: + get: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + - description: account id + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/handler.accountResponce' + summary: Get account information by id + tags: + - account + /api/account/{id}: + delete: + parameters: + - description: id of user + in: path + name: id + required: true + type: integer + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + summary: Delete account by id + tags: + - account + post: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + - description: account info + in: body + name: account + required: true + schema: + $ref: '#/definitions/handler.accountResponce' + - description: account id + in: path + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/handler.accountResponce' + summary: Set account information by id + tags: + - account + /api/account/picture: + post: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + - description: Picture + in: formData + name: file + required: true + type: file + produces: + - application/json + responses: + "200": + description: OK + summary: Get account information + tags: + - account + /api/confidential/: + get: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + summary: Get all confidential settings + tags: + - confidential + post: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + - description: Confidencial settings + in: body + name: confidencial + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/esaccount.AuthorizedUser' + summary: Set confidential settings + tags: + - confidential + /api/confidential/{id}: + get: + parameters: + - description: id of user + in: path + name: id + required: true + type: integer + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + summary: Get all confidential settings by id + tags: + - confidential + post: + parameters: + - description: id of user + in: path + name: id + required: true + type: integer + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + - description: Confidencial settings + in: body + name: confidencial + required: true + schema: + type: string + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/esaccount.AuthorizedUser' + summary: Set confidential settings by id + tags: + - confidential + /api/following/follow/{id}: + put: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + summary: Follow to user + tags: + - following + /api/following/unfollow/{id}: + put: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + required: true + type: string + produces: + - application/json + responses: + "200": + description: OK + summary: Unfollow user + tags: + - following + /api/public/{id}: + get: + parameters: + - description: JWT Bearer token (authorization) + in: header + name: Authorization + type: string + - description: user id + in: query + name: id + required: true + type: integer + produces: + - application/json + responses: + "200": + description: OK + summary: Get all public data of users + tags: + - confidential +swagger: "2.0" diff --git a/pkg/service/service.go b/pkg/service/service.go new file mode 100644 index 0000000..a71f6d5 --- /dev/null +++ b/pkg/service/service.go @@ -0,0 +1,21 @@ +package service + +import ( + "esaccount" +) + +type Authorization interface { + Authorize(header string) (*esaccount.AuthorizedUser, error) + AuthorizeById(id int64) (*esaccount.AuthorizedUser, error) + AuthorizeAndUpdatePicture(header string, picUrl string) (*esaccount.AuthorizedUser, error) +} + +type Service struct { + Authorization +} + +func NewService() *Service { + return &Service{ + Authorization: NewAuthService(), + } +}