/
get.go
93 lines (78 loc) · 2.22 KB
/
get.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright 2019-2020 Axetroy. All rights reserved. MIT license.
package notification
import (
"errors"
"fmt"
"github.com/axetroy/go-server/internal/library/exception"
"github.com/axetroy/go-server/internal/library/helper"
"github.com/axetroy/go-server/internal/library/router"
"github.com/axetroy/go-server/internal/model"
"github.com/axetroy/go-server/internal/schema"
"github.com/axetroy/go-server/internal/service/database"
"github.com/jinzhu/gorm"
"github.com/mitchellh/mapstructure"
"time"
)
// Get notification detail
func Get(c helper.Context, id string) (res schema.Response) {
var (
err error
data schema.Notification
tx *gorm.DB
)
defer func() {
if r := recover(); r != nil {
switch t := r.(type) {
case string:
err = errors.New(t)
case error:
err = t
default:
err = exception.Unknown
}
}
if tx != nil {
if err != nil {
_ = tx.Rollback().Error
} else {
err = tx.Commit().Error
}
}
helper.Response(&res, data, nil, err)
}()
tx = database.Db.Begin()
notificationInfo := model.Notification{}
NotificationMark := model.NotificationMark{Id: id}
sql := fmt.Sprintf("LEFT JOIN notification_mark ON notification_mark.id = notification.id AND notification.id = '%s'", notificationInfo.Id)
if err = tx.Table(notificationInfo.TableName()).Joins(sql).Last(¬ificationInfo).Error; err != nil {
if err == gorm.ErrRecordNotFound {
err = exception.NoData
}
return
}
if er := mapstructure.Decode(notificationInfo, &data.NotificationPure); er != nil {
err = er
return
}
if err = tx.Where(&NotificationMark).Last(&NotificationMark).Error; err != nil {
if err == gorm.ErrRecordNotFound {
data.NotificationPure.Read = false
err = nil
} else {
return
}
} else {
data.NotificationPure.Read = NotificationMark.Read
data.ReadAt = NotificationMark.CreatedAt.Format(time.RFC3339Nano)
}
data.CreatedAt = notificationInfo.CreatedAt.Format(time.RFC3339Nano)
data.UpdatedAt = notificationInfo.UpdatedAt.Format(time.RFC3339Nano)
return
}
// GetRouter get notification detail router
var GetRouter = router.Handler(func(c router.Context) {
id := c.Param("id")
c.ResponseFunc(nil, func() schema.Response {
return Get(helper.NewContext(&c), id)
})
})