| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121 |
- package logic
- import (
- "context"
- "database/sql"
- "errors"
- "time"
- "udeServer/internal/apierr"
- "udeServer/internal/model"
- "udeServer/internal/types"
- )
- func nullString(v string) sql.NullString {
- return sql.NullString{String: v, Valid: v != ""}
- }
- func nullInt64(v int64) sql.NullInt64 {
- return sql.NullInt64{Int64: v, Valid: v != 0}
- }
- func stringValue(v sql.NullString) string {
- if !v.Valid {
- return ""
- }
- return v.String
- }
- func int64Value(v sql.NullInt64) int64 {
- if !v.Valid {
- return 0
- }
- return v.Int64
- }
- func timeValue(t time.Time) string {
- if t.IsZero() {
- return ""
- }
- return t.Format(time.RFC3339)
- }
- func checkId(id int64) error {
- if id <= 0 {
- return apierr.BadRequest("id must be greater than 0")
- }
- return nil
- }
- func findDirectory(ctx context.Context, m model.TDirectoryModel, id int64) (*model.TDirectory, error) {
- data, err := m.FindOne(ctx, id)
- if errors.Is(err, model.ErrNotFound) {
- return nil, apierr.NotFound("directory", id)
- }
- if err != nil {
- return nil, err
- }
- return data, nil
- }
- func findKey(ctx context.Context, m model.TKeyModel, id int64) (*model.TKey, error) {
- data, err := m.FindOne(ctx, id)
- if errors.Is(err, model.ErrNotFound) {
- return nil, apierr.NotFound("key", id)
- }
- if err != nil {
- return nil, err
- }
- return data, nil
- }
- func findService(ctx context.Context, m model.TServiceModel, id int64) (*model.TService, error) {
- data, err := m.FindOne(ctx, id)
- if errors.Is(err, model.ErrNotFound) {
- return nil, apierr.NotFound("service", id)
- }
- if err != nil {
- return nil, err
- }
- return data, nil
- }
- func toDirectoryResp(data *model.TDirectory) *types.DirectoryResp {
- return &types.DirectoryResp{
- Id: data.Id,
- ServiceId: int64Value(data.ServiceId),
- Directory: stringValue(data.Directory),
- }
- }
- func toKeyResp(data *model.TKey) *types.KeyResp {
- return &types.KeyResp{
- Id: data.Id,
- Iden: stringValue(data.Iden),
- Name: stringValue(data.Name),
- Content: stringValue(data.Content),
- Status: stringValue(data.Status),
- CreateAt: timeValue(data.CreateAt),
- }
- }
- func toServiceResp(data *model.TService) *types.ServiceResp {
- return &types.ServiceResp{
- Id: data.Id,
- Name: stringValue(data.Name),
- Ip: stringValue(data.Ip),
- Port: stringValue(data.Port),
- Iden: stringValue(data.Iden),
- KeyIden: stringValue(data.KeyIden),
- KeyAlgo: stringValue(data.KeyAlgo),
- Status: stringValue(data.Status),
- CreateAt: timeValue(data.CreateAt),
- }
- }
|