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

Commit 4592a14

Browse files
committed
added comments
1 parent 0468b18 commit 4592a14

File tree

16 files changed

+49
-11
lines changed

16 files changed

+49
-11
lines changed

authentication-service/README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# mariadb-for-microservices - authentication service
1+
# mariadb-for-microservices - authentication service
2+
The authentication service is responsible for receiving and handling all login requests. The authentication service will return a object to the client containing a token which the client can use to sign requests.
23

3-
## Hoew does this work?
4+
## How does this work?
45
TODO : explain somewhere that we are looking for .env file and environment variables (.env handy for building local)
56

67
## Requirements

authentication-service/app/models/user.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package models
22

33
import "time"
44

5+
// User contains user properties
56
type User struct {
67
ID int `json:"id"`
78
Username string `json:"username"`

comment-service/app/models/comment_create.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package models
22

3+
// CommentCreate is the model for creating a comment
34
type CommentCreate struct {
45
UserID int `json:"user_id"`
56
PhotoID int `json:"photo_id"`

comment-service/database/db.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func CloseConnection(db *sql.DB) {
4141
db.Close()
4242
}
4343

44+
// Create : saves a comment in the database
4445
func Create(db *sql.DB, comment *models.CommentCreate) (*sharedModels.CommentResponse, error) {
4546
res, err := db.Exec("INSERT INTO comments(user_id, photo_id, comment) VALUES(?,?,?)", comment.UserID, comment.PhotoID, comment.Comment)
4647
if err != nil {
@@ -59,6 +60,7 @@ func Create(db *sql.DB, comment *models.CommentCreate) (*sharedModels.CommentRes
5960
return c, nil
6061
}
6162

63+
// GetCommentByID returns a comment from the database
6264
func GetCommentByID(db *sql.DB, id int) (*sharedModels.CommentResponse, error) {
6365
rows, err := db.Query("SELECT id, user_id, photo_id, comment, createdAt FROM comments WHERE id = ?", id)
6466
if err != nil {
@@ -118,6 +120,7 @@ func GetCommentCount(db *sql.DB, items []*sharedModels.CommentCountRequest) ([]*
118120
return responses, nil
119121
}
120122

123+
// GetLastTenComments return the 10 comments for each comment in the [] parameter.
121124
func GetLastTenComments(db *sql.DB, items []*sharedModels.CommentRequest) ([]*sharedModels.CommentResponse, error) {
122125
responses := make([]*sharedModels.CommentResponse, 0)
123126

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/bstaijen/mariadb-for-microservices/shared/util"
2222
)
2323

24+
// CreateHandler create a photo object and store it in the database.
2425
func CreateHandler(connection *sql.DB) negroni.HandlerFunc {
2526
return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
2627
// Get title
@@ -73,6 +74,7 @@ func CreateHandler(connection *sql.DB) negroni.HandlerFunc {
7374
})
7475
}
7576

77+
// IndexHandler serves a photo indentiefied by filename
7678
func IndexHandler(connection *sql.DB) negroni.HandlerFunc {
7779
return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
7880
vars := mux.Vars(r)
@@ -88,6 +90,7 @@ func IndexHandler(connection *sql.DB) negroni.HandlerFunc {
8890
})
8991
}
9092

93+
// ListByUserIDHandler list all photos owned by an user.
9194
func ListByUserIDHandler(connection *sql.DB) negroni.HandlerFunc {
9295
return negroni.HandlerFunc(func(w http.ResponseWriter, r *http.Request, next http.HandlerFunc) {
9396
vars := mux.Vars(r)

photo-service/database/db.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ func OpenConnection(cnf config.Config) (*sql.DB, error) {
2424
log.Debugf("Connect to : %v", dsn)
2525
db, err := sql.Open("mysql", dsn)
2626
if err != nil {
27-
return nil, ErrCanNotConnectWithDatabase
27+
return nil, errCanNotConnectWithDatabase
2828
}
2929

3030
// Open doesn't open a connection. Validate DSN data:
3131
err = db.Ping()
3232
if err != nil {
33-
return nil, ErrCanNotConnectWithDatabase
33+
return nil, errCanNotConnectWithDatabase
3434
}
3535
return db, nil
3636
}
@@ -40,6 +40,7 @@ func CloseConnection(db *sql.DB) {
4040
db.Close()
4141
}
4242

43+
// InsertPhoto : inserts a photo in the database
4344
func InsertPhoto(db *sql.DB, photo *models.CreatePhoto) error {
4445
//Insert
4546
_, err := db.Exec("INSERT INTO photos(user_id, filename, title, contentType, photo) VALUES(?,?,?,?,?)", photo.UserID, photo.Filename, photo.Title, photo.ContentType, photo.Image)
@@ -49,14 +50,17 @@ func InsertPhoto(db *sql.DB, photo *models.CreatePhoto) error {
4950
return nil
5051
}
5152

53+
// ListImagesByUserID returns a list of photo's uploaded by the user.
5254
func ListImagesByUserID(db *sql.DB, id int) ([]*models.Photo, error) {
5355
return selectQuery(db, "SELECT id, user_id, filename, title, createdAt, contentType, photo FROM photos WHERE user_id=?", id)
5456
}
5557

58+
// ListIncoming returns a list of photos ordered by last inserted
5659
func ListIncoming(db *sql.DB, offset int, nrOfRows int) ([]*models.Photo, error) {
5760
return selectQuery(db, "SELECT id, user_id, filename, title, createdAt, contentType, photo FROM photos ORDER BY createdAt DESC LIMIT ?, ?", offset, nrOfRows)
5861
}
5962

63+
// GetPhotoByFilename return a photo based on the filename
6064
func GetPhotoByFilename(db *sql.DB, filename string) (*models.Photo, error) {
6165
photos, err := selectQuery(db, "SELECT id, user_id, filename, title, createdAt, contentType, photo FROM photos WHERE filename = ?", filename)
6266
if len(photos) > 0 {
@@ -65,6 +69,7 @@ func GetPhotoByFilename(db *sql.DB, filename string) (*models.Photo, error) {
6569
return nil, err
6670
}
6771

72+
// GetPhotoById returns a photo indexed by id
6873
func GetPhotoById(db *sql.DB, id int) (*models.Photo, error) {
6974
photos, err := selectQuery(db, "SELECT id, user_id, filename, title, createdAt, contentType, photo FROM photos WHERE id = ?", id)
7075
if len(photos) > 0 {
@@ -94,5 +99,5 @@ func selectQuery(db *sql.DB, query string, args ...interface{}) ([]*models.Photo
9499
return photos, nil
95100
}
96101

97-
// ErrCanNotConnectWithDatabase error if database is unreachable
98-
var ErrCanNotConnectWithDatabase = errors.New("Can not connect with database")
102+
// errCanNotConnectWithDatabase error if database is unreachable
103+
var errCanNotConnectWithDatabase = errors.New("Can not connect with database")

profile-service/app/models/user.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ type User struct {
1919
Hash string
2020
}
2121

22-
// Method of user. (not a function)
23-
// p *user is the receiver of method getUsername()
22+
// GetUsername returns the username of an user
2423
func (u *User) GetUsername() string {
2524
return u.Username
2625
}
@@ -30,7 +29,7 @@ func (u *User) Print() string {
3029
return fmt.Sprintf("%v (%v) - %v", u.Username, u.ID, u.CreatedAt)
3130
}
3231

33-
// Validate method
32+
// Validate returns an error when the username or email is to short.
3433
func (u *User) Validate() error {
3534
if len(u.Username) < 1 {
3635
return ErrUsernameTooShort
@@ -43,6 +42,7 @@ func (u *User) Validate() error {
4342
return nil
4443
}
4544

45+
// ValidatePassword returns an error if the password is to small.
4646
func (u *User) ValidatePassword() error {
4747
if len(u.Password) < 1 {
4848
return ErrPasswordTooShort
@@ -51,6 +51,11 @@ func (u *User) ValidatePassword() error {
5151
return nil
5252
}
5353

54+
// ErrUsernameTooShort is a error and is used when username is too short.
5455
var ErrUsernameTooShort = errors.New("Username is too short")
56+
57+
// ErrEmailTooShort is an error and is used when email address is too short.
5558
var ErrEmailTooShort = errors.New("Email address is to short")
59+
60+
// ErrPasswordTooShort is an error and is used when a password is too short.
5661
var ErrPasswordTooShort = errors.New("Password is to short")

shared/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
# Shared Library
2+
The shared library contains helper methods and modals who are shared with all services.

shared/util/datetime.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77
)
88

9+
// TimeHelper converters a string to a time.Time struct.
910
func TimeHelper(v string) time.Time {
1011
layout := "2006-01-02 15:04:05"
1112
t, err := time.Parse(layout, v)

shared/util/decoder.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"net/http/httptest"
99
)
1010

11+
// RequestToJSON converts a Request containg a json object to a targeted interface
1112
func RequestToJSON(req *http.Request, target interface{}) error {
1213
if req.Body == nil {
1314
return errors.New("Bad json")
@@ -16,6 +17,7 @@ func RequestToJSON(req *http.Request, target interface{}) error {
1617
return toJSON(req.Body, target)
1718
}
1819

20+
// ResponseJSONToObject converts a Response containing a json object to a targeted interface
1921
func ResponseJSONToObject(res *http.Response, target interface{}) error {
2022
if res.Body == nil {
2123
return errors.New("Bad json")
@@ -24,6 +26,7 @@ func ResponseJSONToObject(res *http.Response, target interface{}) error {
2426
return toJSON(res.Body, target)
2527
}
2628

29+
// ResponseRecorderJSONToObject converts a ResponseRecorder containing a json to a targeted interface. This is only used in test cases when a httptest server is used.
2730
func ResponseRecorderJSONToObject(res *httptest.ResponseRecorder, target interface{}) error {
2831
if res.Body == nil {
2932
return errors.New("Bad json")

0 commit comments

Comments
 (0)