Skip to content
This repository was archived by the owner on Jul 20, 2023. It is now read-only.

Commit f29aaab

Browse files
committed
show photo details
1 parent c31fd08 commit f29aaab

File tree

20 files changed

+231
-20
lines changed

20 files changed

+231
-20
lines changed

comment-service/database/db.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ func GetComments(db *sql.DB, photoID, offset, nrOfRows int) ([]*sharedModels.Com
9090
return responses, nil
9191
}
9292

93+
// GetCommentsByUserID get all comments from a user.
9394
func GetCommentsByUserID(db *sql.DB, userID, offset, nrOfRows int) ([]*sharedModels.CommentResponse, error) {
9495
rows, err := db.Query("SELECT id, user_id, photo_id, comment, createdAt FROM comments WHERE user_id=? ORDER BY createdAt DESC LIMIT ?, ?", userID, offset, nrOfRows)
9596
if err != nil {

photo-service/app/http/controllers/image_controller.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"strings"
1010
"time"
1111

12+
"github.com/Sirupsen/logrus"
1213
"github.com/bstaijen/mariadb-for-microservices/photo-service/config"
1314
"github.com/gorilla/mux"
1415
"github.com/urfave/negroni"
@@ -114,6 +115,39 @@ func ListByUserIDHandler(connection *sql.DB, cnf config.Config) negroni.HandlerF
114115
})
115116
}
116117

118+
func GetPhotoByID(connection *sql.DB, cnf config.Config) negroni.HandlerFunc {
119+
return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
120+
121+
userID, err := getUserIDFromRequest(cnf, r)
122+
if err != nil {
123+
util.SendError(w, err)
124+
return
125+
}
126+
127+
vars := mux.Vars(r)
128+
strID := vars["id"]
129+
id, err := strconv.Atoi(strID)
130+
if err != nil {
131+
util.SendErrorMessage(w, fmt.Sprintf("id must be integer, instead got %v", id))
132+
return
133+
}
134+
135+
photo, err := db.GetPhotoById(connection, id)
136+
if err != nil {
137+
util.SendError(w, err)
138+
return
139+
}
140+
141+
logrus.Info(photo.ID)
142+
143+
photos := make([]*models.Photo, 0)
144+
photos = append(photos, photo)
145+
photos = findResources(cnf, photos, userID, true, true, true)
146+
147+
util.SendOK(w, photos[0])
148+
})
149+
}
150+
117151
func randomFileName() string {
118152
rand.Seed(time.Now().UTC().UnixNano())
119153
var res string

photo-service/app/http/controllers/incoming_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -599,7 +599,7 @@ func getUserIDFromRequest(cnf config.Config, req *http.Request) (int, error) {
599599
}
600600

601601
if len(queryToken) < 1 {
602-
return 0, errors.New("No JWT available")
602+
return 0, errors.New("No token available")
603603
}
604604

605605
tok, err := jwt.Parse(queryToken, func(t *jwt.Token) (interface{}, error) {

photo-service/app/http/routes/routes.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ func setPhotoRoutes(db *sql.DB, cnf config.Config, router *mux.Router) *mux.Rout
7070
controllers.HotHandler(db, cnf),
7171
)).Methods("GET")
7272

73+
// Add image for user /image/{id}
74+
image.Handle("/{id}", negroni.New(
75+
negroni.HandlerFunc(middleware.AccessControlHandler),
76+
controllers.GetPhotoByID(db, cnf),
77+
)).Methods("GET")
78+
7379
// Subrouter /images/{file}
7480
images := router.PathPrefix("/images/{file}").Subrouter()
7581

photo-service/app/models/photo.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,8 @@ type Photo struct {
2121
YouDownvote bool `json:"downvote"`
2222
Comments []*sharedModels.CommentResponse `json:"comments"`
2323
CommentCount int `json:"comment_count"`
24-
25-
ContentType string
26-
Image []byte
24+
ContentType string `json:"-"`
25+
Image []byte `json:"-"`
2726
}
2827

2928
// CreatePhoto can be used for creating a new photo object

photo-service/database/db.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,21 @@ func GetPhotoByFilename(db *sql.DB, filename string) (*models.Photo, error) {
6868
if len(photos) > 0 {
6969
return photos[0], err
7070
}
71-
return nil, err
71+
return nil, err
7272
}
7373

7474
// GetPhotoById returns a photo indexed by id
7575
func GetPhotoById(db *sql.DB, id int) (*models.Photo, error) {
76-
photos, err := selectQuery(db, "SELECT id, user_id, filename, title, createdAt, contentType, photo FROM photos WHERE id = ?", id)
76+
photos, err := selectQuery(db, "SELECT id, user_id, filename, title, createdAt, contentType, photo FROM photos WHERE id = ?", id)
77+
78+
log.Info(photos)
79+
7780
if len(photos) > 0 {
7881
return photos[0], err
82+
} else if len(photos) == 0 {
83+
return nil, errors.New("photo not found")
7984
}
80-
return nil, nil
85+
return nil, err
8186
}
8287

8388
func GetPhotos(db *sql.DB, items []*sharedModels.PhotoRequest) ([]*sharedModels.PhotoResponse, error) {

vote-service/app/http/controllers/controllers.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ func GetVotesFromAUser(connection *sql.DB, cnf config.Config) negroni.HandlerFun
141141
})
142142
}
143143

144+
func HealthHandler(connection *sql.DB, cnf config.Config) negroni.HandlerFunc {
145+
return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
146+
147+
log.Println("Health Handler called")
148+
149+
util.SendOKMessage(w, "I am healthy")
150+
151+
})
152+
}
153+
144154
func getPhotos(cnf config.Config, input []*sharedModels.TopRatedPhotoResponse) []*sharedModels.PhotoResponse {
145155
type Req struct {
146156
Requests []*sharedModels.TopRatedPhotoResponse `json:"requests"`

vote-service/app/http/routes/routes.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ func InitRoutes(db *sql.DB, cnf config.Config) *mux.Router {
2020
}
2121

2222
func setRESTRoutes(db *sql.DB, cnf config.Config, router *mux.Router) *mux.Router {
23+
health := router.PathPrefix("/health").Subrouter()
24+
health.Methods("OPTIONS").Handler(negroni.New(
25+
negroni.HandlerFunc(middleware.AcceptOPTIONS),
26+
))
27+
health.Methods("GET").Handler(negroni.New(
28+
negroni.HandlerFunc(middleware.AccessControlHandler),
29+
controllers.HealthHandler(db, cnf),
30+
))
31+
2332
votes := router.PathPrefix("/votes").Subrouter()
2433
votes.Methods("OPTIONS").Handler(negroni.New(
2534
negroni.HandlerFunc(middleware.AcceptOPTIONS),

webserver/webapp/app.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ var app = angular.module("MariaDBApp", ['ngRoute', 'ngFileUpload', 'ui.bootstrap
5151
controller: "UserController",
5252
activetab: 'user'
5353
})
54+
.when("/photo/:id",{
55+
templateUrl: "view/PhotoDetailsView.html",
56+
controller: "PhotoDetailsController",
57+
activetab: 'photo'
58+
})
5459
.otherwise({
5560
redirectTo: "/"
5661
});

webserver/webapp/assets/css/style.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ body {
9292
}
9393

9494
/* Alter the height of the navbar */
95+
.navbar-toggle{ margin-top: 18px;margin-bottom: 18px;}
9596
.navbar-nav > li > a {padding-top:30px !important; padding-bottom:30px !important;}
9697
.navbar-form {margin-top:18px !important; margin-bottom: 18px !important;}
9798
.navbar-brand {

0 commit comments

Comments
 (0)