-
Notifications
You must be signed in to change notification settings - Fork 1
/
userController.go
99 lines (76 loc) · 2.58 KB
/
userController.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
package controller
import (
"context"
"fmt"
"net/http"
service "github.com/t-revathi/revenuecalculatorservice/service/userservice"
"github.com/go-chi/chi"
"github.com/go-chi/render"
)
type DBInterface interface {
Pingdb(context.Context) error
Insertone(ctx context.Context, dataBase string, col string, doc interface{}) *service.InsertOneResult
FindAll(ctx context.Context, dataBase string, col string, filter interface{}) *string
}
type UserController struct {
userService *service.UserService
dbService DBInterface
}
func NewUserController(savetodb string, dburi string, dbinterface DBInterface) *UserController {
return &UserController{
userService: service.NewUserService(),
dbService: dbinterface,
}
}
func Health(next http.Handler) http.Handler {
fn := func(w http.ResponseWriter, r *http.Request) {
fmt.Println(r.RequestURI)
next.ServeHTTP(w, r)
}
return http.HandlerFunc(fn)
}
func (u *UserController) WireRoutes(r chi.Router) {
r.Route("/", func(r chi.Router) {
r = r.With(Health)
r.Post("/calculateRevenue", u.PerformCalculateProfit)
r.Get("/healthCheck", u.healthCheck)
r.Get("/allRevenue", u.ShowAllRevenue)
})
}
func (u *UserController) PerformCalculateProfit(w http.ResponseWriter, r *http.Request) {
var dataCalculateRevenue service.DataCalculateRevenue
if err := render.DecodeJSON(r.Body, &dataCalculateRevenue); err != nil {
return
}
result := u.userService.PerformCalculateProfit(r.Context(), w, r, &dataCalculateRevenue)
// Insert and Listing opertaions
dbname := "stockprofitcalculator"
collection := "plResults"
res := u.dbService.Insertone(r.Context(), dbname, collection, result.Items)
if res.Err != nil {
fmt.Println("Error Occured during insertion" + res.Err.Error())
}
fmt.Println(res, res.Err)
//Listing the last inserted Record
// id, _ := res.Result.(primitive.ObjectID)
// //primitive.ObjectIDFromHex(string("62ccdf87b79b0e2fc4ea67f0"))
// filter := bson.D{{Key: "_id", Value: id}}
// var record bson.M
// u.dbService.FindOne(&u.dbService.Client, u.dbService.Ctx, dbname, collection, filter).Decode(&record)
// fmt.Println(record)
//Return results to client
render.JSON(w, r,
result.Items)
}
func (u *UserController) ShowAllRevenue(w http.ResponseWriter, r *http.Request) {
collection := "plResults"
// // Get all records
filter := ""
results := u.dbService.FindAll(r.Context(), "stockprofitcalculator", collection, filter)
fmt.Println(results)
// //Return results to client
render.JSON(w, r, results)
}
func (u *UserController) healthCheck(w http.ResponseWriter, r *http.Request) {
w.Write([]byte("Healthcheck good"))
}