Skip to content

Commit

Permalink
Merge pull request #37 from titouanfreville/issue-30-secureAPI
Browse files Browse the repository at this point in the history
Added user parameter routes
  • Loading branch information
titouanfreville authored Mar 12, 2017
2 parents af86fce + f729f7b commit 72ce964
Show file tree
Hide file tree
Showing 19 changed files with 1,010 additions and 429 deletions.
24 changes: 21 additions & 3 deletions api/api_parameters.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,29 @@ type avatarIDParam struct {
AvatarID int `json:"avatarID"`
}

// swagger:parameters newAvatar newAvatar1 updateAvatar
// type avatarObjectParam struct {
// //Link of the avatar in server.
// // in:body
// Avatar models.Avatar `json:""`
// }

// avatarObjectParam object
//
// Avatar object to provide to update or create avatar into the database.
//
// swagger:parameters newAvatar newAvatar1 updateAvatar
type avatarObjectParam struct {
//Link of the avatar in server.
// in:body
Avatar models.Avatar `json:"avatar"`
// Avatar name
//
// required: true
// in: body
Name string `gorm:"column:name;not null;unique" json:"name,omitempty"`
// Path into server
//
// required: true
// in: body
Link string `gorm:"column:link;not null;unique" json:"link,omitempty"`
}

// <><><><><> <><><><><> <><><><><> <><><><><> //
Expand Down
24 changes: 9 additions & 15 deletions api/avatar_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,7 @@ func getAvatarFromLink(w http.ResponseWriter, r *http.Request) {
}

func newAvatar(w http.ResponseWriter, r *http.Request) {
var data struct {
Avatar *models.Avatar
OmitID interface{} `json:"id,omitempty"`
}
var Avatar *models.Avatar
token := r.Context().Value(jwtTokenKey).(*jwt.Token)
if !canManageOrganisation(token) {
res := error401
Expand All @@ -196,28 +193,25 @@ func newAvatar(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()
db := dbStore.db
request := r.Body
err := chiRender.Bind(request, &data)
if err != nil || data.Avatar == nil {
err := chiRender.Bind(request, &Avatar)
if err != nil || Avatar == nil {
render.JSON(w, error422.StatusCode, error422)
return
}
if err := db.DB().Ping(); err != nil {
render.JSON(w, error503.StatusCode, error503)
return
}
rerr := store.Avatar().Save(data.Avatar, db)
rerr := store.Avatar().Save(Avatar, db)
if rerr != nil {
render.JSON(w, rerr.StatusCode, rerr)
return
}
render.JSON(w, 201, data.Avatar)
render.JSON(w, 201, Avatar)
}

func updateAvatar(w http.ResponseWriter, r *http.Request) {
var data struct {
Avatar *models.Avatar
OmitID interface{} `json:"id,omitempty"`
}
var Avatar *models.Avatar
token := r.Context().Value(jwtTokenKey).(*jwt.Token)
if !canManageOrganisation(token) {
res := error401
Expand All @@ -228,17 +222,17 @@ func updateAvatar(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()
db := dbStore.db
request := r.Body
err := chiRender.Bind(request, &data)
err := chiRender.Bind(request, &Avatar)
avatar := r.Context().Value(oldAvatarKey).(models.Avatar)
if err != nil || data.Avatar == nil {
if err != nil || &Avatar == nil {
render.JSON(w, error422.StatusCode, error422)
return
}
if err := db.DB().Ping(); err != nil {
render.JSON(w, error503.StatusCode, error503)
return
}
rerr := store.Avatar().Update(&avatar, data.Avatar, db)
rerr := store.Avatar().Update(&avatar, Avatar, db)
if err != nil {
render.JSON(w, rerr.StatusCode, rerr)
return
Expand Down
85 changes: 47 additions & 38 deletions api/emojis_route.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,26 +177,35 @@ func getAllEmoji(w http.ResponseWriter, r *http.Request) {

func getEmojiFromName(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()

db := dbStore.db
if err := db.DB().Ping(); err != nil {
render.JSON(w, error503.StatusCode, error503)
return
}
name := r.Context().Value(emojiNameKey).(string)
emoji := store.Emoji().GetByName(name, db)
render.JSON(w, 200, emoji)
}

func getEmojiFromShortcut(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()

db := dbStore.db
if err := db.DB().Ping(); err != nil {
render.JSON(w, error503.StatusCode, error503)
return
}
link := r.Context().Value(emojiShortcutKey).(string)
emoji := store.Emoji().GetByShortcut(link, db)
render.JSON(w, 200, emoji)
}

func getEmojiFromLink(w http.ResponseWriter, r *http.Request) {
store := datastores.Store()

db := dbStore.db
if err := db.DB().Ping(); err != nil {
render.JSON(w, error503.StatusCode, error503)
return
}
link := r.Context().Value(emojiLinkKey).(string)
emoji := store.Emoji().GetByLink(link, db)
render.JSON(w, 200, emoji)
Expand All @@ -220,18 +229,19 @@ func newEmoji(w http.ResponseWriter, r *http.Request) {
err := chiRender.Bind(request, &data)
if err != nil || data.Emoji == nil {
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Emoji().Save(data.Emoji, db)
if err == nil {
render.JSON(w, 201, data.Emoji)
} else {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, error503.StatusCode, error503)
}
return
}

if err := db.DB().Ping(); err != nil {
render.JSON(w, error503.StatusCode, error503)
return
}
apperr := store.Emoji().Save(data.Emoji, db)
if apperr != nil {
render.JSON(w, apperr.StatusCode, apperr)
return
}
render.JSON(w, 201, data.Emoji)
}

func updateEmoji(w http.ResponseWriter, r *http.Request) {
Expand All @@ -253,18 +263,17 @@ func updateEmoji(w http.ResponseWriter, r *http.Request) {
emoji := r.Context().Value(oldEmojiKey).(models.Emoji)
if err != nil || data.Emoji == nil {
render.JSON(w, error422.StatusCode, error422)
} else {
if err := db.DB().Ping(); err == nil {
err := store.Emoji().Update(&emoji, data.Emoji, db)
if err == nil {
render.JSON(w, 200, emoji)
} else {
render.JSON(w, err.StatusCode, err)
}
} else {
render.JSON(w, error503.StatusCode, error503)
}
}
if err := db.DB().Ping(); err != nil {
render.JSON(w, error503.StatusCode, error503)
return
}
apperr := store.Emoji().Update(&emoji, data.Emoji, db)
if apperr != nil {
render.JSON(w, apperr.StatusCode, apperr)
return
}
render.JSON(w, 200, emoji)
}

func deleteEmoji(w http.ResponseWriter, r *http.Request) {
Expand All @@ -281,18 +290,18 @@ func deleteEmoji(w http.ResponseWriter, r *http.Request) {
Object: emoji,
}
db := dbStore.db
if err := db.DB().Ping(); err == nil {
err := store.Emoji().Delete(&emoji, db)
if err == nil {
message.Success = true
message.Message = "Emoji well removed."
render.JSON(w, 200, message)
} else {
message.Success = false
message.Message = err.Message
render.JSON(w, err.StatusCode, message.Message)
}
} else {
render.JSON(w, 503, error503)
if err := db.DB().Ping(); err != nil {
render.JSON(w, error503.StatusCode, error503)
return
}
apperr := store.Emoji().Delete(&emoji, db)
if apperr != nil {
message.Success = false
message.Message = apperr.Message
render.JSON(w, apperr.StatusCode, message.Message)
return
}
message.Success = true
message.Message = "Emoji well removed."
render.JSON(w, 200, message)
}
Loading

0 comments on commit 72ce964

Please sign in to comment.