-
Notifications
You must be signed in to change notification settings - Fork 0
/
offer.go
155 lines (137 loc) · 4.07 KB
/
offer.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package main
import (
"crypto/rsa"
"errors"
"fmt"
"forent_api/app"
"forent_api/models"
"io/ioutil"
jwtgo "github.com/dgrijalva/jwt-go"
"github.com/goadesign/goa"
"github.com/goadesign/goa/middleware/security/jwt"
"github.com/jinzhu/gorm"
)
// OfferController implements the offer resource.
type OfferController struct {
*goa.Controller
privateKey *rsa.PrivateKey
}
// NewOfferController creates a offer controller.
func NewOfferController(service *goa.Service) (*OfferController, error) {
b, err := ioutil.ReadFile("./jwtkey/jwt.key")
if err != nil {
return nil, err
}
privKey, err := jwtgo.ParseRSAPrivateKeyFromPEM(b)
if err != nil {
return nil, fmt.Errorf("jwt: failed to load private key: %s", err) // bug
}
return &OfferController{
Controller: service.NewController("OfferController"),
privateKey: privKey,
}, nil
}
// Create runs the create action.
func (c *OfferController) Create(ctx *app.CreateOfferContext) error {
payload := ctx.Payload
// Retrieve the token claims
token := jwt.ContextJWT(ctx)
if token == nil {
return fmt.Errorf("JWT token is missing from context") // internal error
}
if claims, ok := token.Claims.(jwtgo.MapClaims); ok && token.Valid {
item, _ := ItemDB.OneItem(ctx.Context, payload.ItemID, 0, 0, 0)
if userID, ok := claims["user_id"].(float64); ok {
offer := models.Offer{}
offer.UserID = int(userID)
offer.ItemID = payload.ItemID
offer.OwnerID = item.UserID
offer.Price = payload.Price
offer.StartAt = payload.StartAt
offer.EndAt = payload.EndAt
err := OfferDB.Add(ctx.Context, &offer)
if err != nil {
return ErrDatabaseError(err)
}
return ctx.Created()
}
}
errID := errors.New("id error")
return ctx.BadRequest(errID)
}
// List runs the list action.
func (c *OfferController) List(ctx *app.ListOfferContext) error {
payload := ctx.Payload
// Retrieve the token claims
token := jwt.ContextJWT(ctx)
if token == nil {
return fmt.Errorf("JWT token is missing from context") // internal error
}
if claims, ok := token.Claims.(jwtgo.MapClaims); ok && token.Valid {
if userID, ok := claims["user_id"].(float64); ok {
// input offer id
var objs []*app.Offer
if payload.OfferID != 0 {
offer, _ := OfferDB.OneOfferByID(ctx.Context, payload.OfferID)
if offer.UserID == int(userID) {
profile, _ := ProfileDB.OneProfilebyUseID(ctx.Context, offer.OwnerID)
offer.Profile = profile
objs = append(objs, offer)
return ctx.OK(objs)
}
if offer.OwnerID == int(userID) {
profile, _ := ProfileDB.OneProfilebyUseID(ctx.Context, offer.UserID)
offer.Profile = profile
objs = append(objs, offer)
return ctx.OK(objs)
}
errID := errors.New("id error")
return ctx.BadRequest(errID)
}
offeres := OfferDB.ListOfferPreload(ctx.Context, int(userID))
for _, offer := range offeres {
if offer.UserID == int(userID) {
profile, _ := ProfileDB.OneProfilebyUseID(ctx.Context, offer.OwnerID)
offer.Profile = profile
}
if offer.OwnerID == int(userID) {
profile, _ := ProfileDB.OneProfilebyUseID(ctx.Context, offer.UserID)
offer.Profile = profile
}
objs = append(objs, offer)
}
return ctx.OK(objs)
}
}
errID := errors.New("id error")
return ctx.BadRequest(errID)
}
// Accept runs the accept action.
func (c *OfferController) Accept(ctx *app.AcceptOfferContext) error {
payload := ctx.Payload
// Retrieve the token claims
token := jwt.ContextJWT(ctx)
if token == nil {
return fmt.Errorf("JWT token is missing from context") // internal error
}
if claims, ok := token.Claims.(jwtgo.MapClaims); ok && token.Valid {
if userID, ok := claims["user_id"].(float64); ok {
offer, err := OfferDB.Get(ctx.Context, payload.OfferID)
if err == gorm.ErrRecordNotFound {
return ctx.NotFound()
}
if offer.OwnerID != int(userID) {
errID := errors.New("id error")
return ctx.BadRequest(errID)
}
offer.Accepted = true
err = OfferDB.Update(ctx, offer)
if err != nil {
return ErrDatabaseError(err)
}
return ctx.NoContent()
}
}
errID := errors.New("id error")
return ctx.BadRequest(errID)
}