Skip to content

Commit

Permalink
Merge pull request #56 from PierreBougon/develop
Browse files Browse the repository at this point in the history
Release 1.01
  • Loading branch information
PierreBougon authored Dec 4, 2019
2 parents b240757 + b9b0327 commit 91d660b
Show file tree
Hide file tree
Showing 32 changed files with 160 additions and 121 deletions.
6 changes: 3 additions & 3 deletions app/auth.go → app/auth/auth.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package app
package auth

import (
"fmt"
"github.com/PierreBougon/Bym-BackEnd/models"
u "github.com/PierreBougon/Bym-BackEnd/utils"
"github.com/PierreBougon/Bym-BackEnd/app/models"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"

"context"
"net/http"
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package controllers

import (
"encoding/json"
"github.com/PierreBougon/Bym-BackEnd/models"
u "github.com/PierreBougon/Bym-BackEnd/utils"
"github.com/PierreBougon/Bym-BackEnd/app/models"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"
"net/http"
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package controllers

import (
"github.com/PierreBougon/Bym-BackEnd/models"
u "github.com/PierreBougon/Bym-BackEnd/utils"
"github.com/PierreBougon/Bym-BackEnd/app/models"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"

"encoding/json"
"net/http"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package controllers

import (
"fmt"
"github.com/PierreBougon/Bym-BackEnd/models"
u "github.com/PierreBougon/Bym-BackEnd/utils"
"github.com/PierreBougon/Bym-BackEnd/websocket"
"github.com/PierreBougon/Bym-BackEnd/app/models"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"
"github.com/PierreBougon/Bym-BackEnd/app/websocket"

"encoding/json"
"github.com/gorilla/mux"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package controllers

import (
"github.com/PierreBougon/Bym-BackEnd/models"
u "github.com/PierreBougon/Bym-BackEnd/utils"
"github.com/PierreBougon/Bym-BackEnd/app/models"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"
"github.com/gorilla/mux"
"net/http"
"strconv"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package controllers

import (
"github.com/PierreBougon/Bym-BackEnd/models"
u "github.com/PierreBougon/Bym-BackEnd/utils"
"github.com/PierreBougon/Bym-BackEnd/websocket"
"github.com/PierreBougon/Bym-BackEnd/app/models"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"
"github.com/PierreBougon/Bym-BackEnd/app/websocket"
"github.com/gorilla/mux"

"encoding/json"
Expand All @@ -30,6 +30,7 @@ var CreateSong = func(w http.ResponseWriter, r *http.Request) {
var GetSongs = func(w http.ResponseWriter, r *http.Request) {
var param string

user := r.Context().Value("user").(uint)
vals := r.URL.Query() // Returns a url.Values, which is a map[string][]string
playlistId, ok := vals["playlist_id"] // Note type, not ID. ID wasn't specified anywhere.

Expand All @@ -49,7 +50,7 @@ var GetSongs = func(w http.ResponseWriter, r *http.Request) {
return
}

data := models.GetSongs(uint(plistid))
data := models.GetSongs(uint(plistid), user)
if data == nil {
u.RespondBadRequestWithMessage(w, "Invalid request, playlist Id doesn't match with any playlist")
return
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package controllers
import (
"encoding/json"
"fmt"
"github.com/PierreBougon/Bym-BackEnd/models"
u "github.com/PierreBougon/Bym-BackEnd/utils"
"github.com/PierreBougon/Bym-BackEnd/app/models"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"
"github.com/PierreBougon/Bym-BackEnd/app/websocket"
"github.com/gorilla/mux"
"net"
"net/http"
Expand Down Expand Up @@ -60,9 +61,9 @@ var UpdateOrCreateVote = func(w http.ResponseWriter, r *http.Request) {

resp := u.Message(true, "This request has performed no action")
if vote.UpVote == true {
resp = models.UpVoteSong(uint(songId), user)
resp = models.UpVoteSong(uint(songId), user, websocket.NotifyPlaylistSubscribers, websocket.PlaylistNeedRefresh)
} else if vote.DownVote == true {
resp = models.DownVoteSong(uint(songId), user)
resp = models.DownVoteSong(uint(songId), user, websocket.NotifyPlaylistSubscribers, websocket.PlaylistNeedRefresh)
}
u.Respond(w, resp)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package controllers

import (
"fmt"
"github.com/PierreBougon/Bym-BackEnd/websocket"
"github.com/PierreBougon/Bym-BackEnd/app/websocket"
"net/http"
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ import (
)

type PlaylistAccessControl struct {
CreatedAt time.Time
UpdatedAt time.Time
UserId uint `gorm:"unique_index:idx_user_playlist",json:"user_id"`
PlaylistId uint `gorm:"unique_index:idx_user_playlist",json:"playlist_id"`
RoleId uint `json:"role_id"`
CreatedAt time.Time
UpdatedAt time.Time
UserId uint `gorm:"unique_index:idx_user_playlist",json:"user_id"`
PlaylistId uint `gorm:"unique_index:idx_user_playlist",json:"playlist_id"`
RoleId uint `json:"role_id"`
}

func checkRight(user uint, playlist uint, neededRole uint) bool {
Expand All @@ -32,6 +32,6 @@ func checkRight(user uint, playlist uint, neededRole uint) bool {
fmt.Println("User does not have any right on this playlist.")
return false
}
fmt.Println("Current role on this playlist : " + string(acl.RoleId) + " Result : " + strconv.FormatBool(acl.RoleId <= neededRole))
fmt.Println("Current role on this playlist : " + string(acl.RoleId) + " Result : " + strconv.FormatBool(acl.RoleId <= neededRole))
return acl.RoleId <= neededRole
}
}
File renamed without changes.
16 changes: 8 additions & 8 deletions models/accounts.go → app/models/accounts.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package models

import (
u "github.com/PierreBougon/Bym-BackEnd/utils"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"

"os"
"strings"
Expand All @@ -23,13 +23,13 @@ type Token struct {
//a struct to rep user account
type Account struct {
Model
Email string `json:"email",gorm:"type:text;unique;not null"`
Password string `json:"password"`
TokenVersion uint `json:"token_version"`
Picture string `json:"picture"`
Playlists []Playlist `gorm:"ForeignKey:UserId"`
FollowedPlaylists []*Playlist `gorm:"many2many:account_playlist"`
Acl []PlaylistAccessControl `gorm:"ForeignKey:UserId"`
Email string `json:"email",gorm:"type:text;unique;not null"`
Password string `json:"password"`
TokenVersion uint `json:"token_version"`
Picture string `json:"picture"`
Playlists []Playlist `gorm:"ForeignKey:UserId"`
FollowedPlaylists []*Playlist `gorm:"many2many:account_playlist"`
Acl []PlaylistAccessControl `gorm:"ForeignKey:UserId"`
}

func (account *Account) ValidatePassword() (map[string]interface{}, bool) {
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 3 additions & 3 deletions models/playlists.go → app/models/playlists.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package models

import (
"fmt"
u "github.com/PierreBougon/Bym-BackEnd/utils"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"
"github.com/jinzhu/gorm"
)

Expand Down Expand Up @@ -227,13 +227,13 @@ func updatePlaylistSoundCount(user uint, playlistId uint, countModifier int) map
return u.Message(true, "Playlist successfully updated")
}

func (playlist *Playlist) DeletePlaylist(user uint, playlistId uint, notifyOnDelete func(playlistId uint, userId uint, message string), messageOnDelete string) map[string]interface{} {
func (playlist *Playlist) DeletePlaylist(user uint, playlistId uint, notifyOnDelete func(userId uint, playlistId uint, message string), messageOnDelete string) map[string]interface{} {
retPlaylist := &Playlist{}
err := db.Where(&Playlist{UserId: user}).First(&retPlaylist, playlistId).Error
if err != nil {
return u.Message(false, "Invalid playlist, you may not own this playlist")
}
notifyOnDelete(playlistId, user, messageOnDelete)
notifyOnDelete(user, playlistId, messageOnDelete)
db.Model(retPlaylist).Association("Follower").Clear()
db.Model(retPlaylist).Association("Acl").Clear()
db.Delete(retPlaylist)
Expand Down
12 changes: 6 additions & 6 deletions models/roles.go → app/models/roles.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
package models

type Role struct {
ID uint `gorm:"primary_key"`
Name string `json:"name"`
Acl []PlaylistAccessControl `gorm:"ForeignKey:RoleId"`
ID uint `gorm:"primary_key"`
Name string `json:"name"`
Acl []PlaylistAccessControl `gorm:"ForeignKey:RoleId"`
// We can add image + infos etc
}

// const ROLE_DEV =
const ROLE_ADMIN = 1
const ROLE_BYMER = 2
const ROLE_FOLLOWER = 3
const ROLE_VISITOR = 4
const ROLE_FOLLOWER = 3
const ROLE_VISITOR = 4

var RoleName = [5]string{
"Author",
"Admin",
"Bymer",
"Follower",
"Visitor",
}
}
44 changes: 32 additions & 12 deletions models/songs.go → app/models/songs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package models

import (
"fmt"
u "github.com/PierreBougon/Bym-BackEnd/utils"
u "github.com/PierreBougon/Bym-BackEnd/app/utils"
"sort"
)

Expand Down Expand Up @@ -50,7 +50,7 @@ func (song *Song) Validate(user uint) (map[string]interface{}, bool) {
return u.Message(true, "Requirement passed"), true
}

func (song *Song) Create(user uint, notifyOnDelete func(playlistId uint, userId uint, message string), messageOnUpdate func(playlistId uint, userId uint) string) map[string]interface{} {
func (song *Song) Create(user uint, notifyOnUpdate func(userId uint, playlistId uint, message string), messageOnUpdate func(playlistId uint, userId uint) string) map[string]interface{} {

song.Status = "NONE"
if resp, ok := song.Validate(user); !ok {
Expand All @@ -66,24 +66,43 @@ func (song *Song) Create(user uint, notifyOnDelete func(playlistId uint, userId
return u.Message(false, "Failed to create song, connection error.")
}
updatePlaylistSoundCount(user, song.PlaylistId, 1)
notifyOnDelete(song.PlaylistId, user, messageOnUpdate(song.PlaylistId, user))
notifyOnUpdate(user, song.PlaylistId, messageOnUpdate(song.PlaylistId, user))
response := u.Message(true, "song has been created")
response["song"] = song
return response
}

func GetSongs(playlist uint) []*SongExtended {
func GetSongs(playlist uint, userId uint) []*SongExtended {
songs := make([]*SongExtended, 0)
err := GetDB().Table("songs").
Select("songs.*, Coalesce(votes.up_vote, votes.down_vote) as personal_vote").
Joins("LEFT JOIN votes ON votes.song_id = songs.id").
Select("songs.*").
//Joins("LEFT JOIN votes ON votes.song_id = songs.id").
Where("playlist_id = ?", playlist).
Find(&songs).Error
Order("songs.score DESC").
Find(&songs).
Error

//
if err != nil {
fmt.Println(err)
return nil
}

// TODO : Strongly unoptimized process we need to check a new process to retrieve personal votes
// Maybe add playlist_id in votes to be able to get all the votes from a playlist ?
// Or find a way to inject this logic directly in a single sql request (care not to produce duplicates)
vote := &Vote{}
for _, song := range songs {
vote = GetPersonalVoteBySongId(song.ID, userId)
if vote != nil && vote.UpVote == true {
song.PersonalVote = &vote.UpVote
} else if vote != nil && vote.DownVote == true {
song.PersonalVote = &vote.DownVote
} else {
song.PersonalVote = nil
}
}

//songs = pushFrontPlayingSong(songs)
songs = pushFrontPlayedSongs(songs)
return songs
Expand Down Expand Up @@ -145,7 +164,7 @@ func pushFrontPlayedSongs(songs []*SongExtended) []*SongExtended {
return songs
}

func (song *Song) UpdateSong(user uint, songId uint, newSong *Song, notifyOnDelete func(playlistId uint, userId uint, message string), messageOnUpdate func(playlistId uint, userId uint) string) map[string]interface{} {
func (song *Song) UpdateSong(user uint, songId uint, newSong *Song, notifyOnUpdate func(userId uint, playlistId uint, message string), messageOnUpdate func(playlistId uint, userId uint) string) map[string]interface{} {
retSong := &Song{}
err := db.First(&retSong, songId).Error
playlist := &Playlist{}
Expand All @@ -169,7 +188,7 @@ func (song *Song) UpdateSong(user uint, songId uint, newSong *Song, notifyOnDele
retSong.Status = newSong.Status
}
db.Save(&retSong)
notifyOnDelete(playlist.ID, user, messageOnUpdate(playlist.ID, user))
notifyOnUpdate(user, retSong.PlaylistId, messageOnUpdate(retSong.PlaylistId, user))
return u.Message(true, "Song successfully updated")
}

Expand All @@ -180,7 +199,7 @@ func isStatusValid(status string) bool {
return false
}

func (song *Song) DeleteSong(user uint, songId uint, notifyOnDelete func(playlistId uint, userId uint, message string), messageOnUpdate func(playlistId uint, userId uint) string) map[string]interface{} {
func (song *Song) DeleteSong(user uint, songId uint, notifyOnDelete func(userId uint, playlistId uint, message string), messageOnUpdate func(playlistId uint, userId uint) string) map[string]interface{} {
retSong := &Song{}
err := db.First(&retSong, songId).Error
playlist := GetPlaylistFromSong(retSong)
Expand All @@ -189,7 +208,7 @@ func (song *Song) DeleteSong(user uint, songId uint, notifyOnDelete func(playlis
}
db.Delete(&retSong)
updatePlaylistSoundCount(user, song.PlaylistId, -1)
notifyOnDelete(playlist.ID, user, messageOnUpdate(playlist.ID, user))
notifyOnDelete(user, playlist.ID, messageOnUpdate(playlist.ID, user))
return u.Message(true, "Song successfully deleted")
}

Expand Down Expand Up @@ -236,7 +255,7 @@ func GetSongRankingById(songid uint) *Ranking {
return &rank
}

func RefreshSongVotes(songid uint) {
func RefreshSongVotes(userId uint, songid uint, notifyOnUpdate func(userId uint, playlistId uint, message string), messageOnUpdate func(playlistId uint, userId uint) string) {
//TODO : should now be threaded in a goroutine need a feedback to be sure it's fully working

// votes := make([]*Vote, 0)
Expand All @@ -256,4 +275,5 @@ func RefreshSongVotes(songid uint) {
song.VoteDown = downVotes
song.Score = upVotes*100 - downVotes*100
db.Save(&song)
notifyOnUpdate(userId, song.PlaylistId, messageOnUpdate(song.PlaylistId, userId))
}
2 changes: 1 addition & 1 deletion models/tableInterface.go → app/models/tableInterface.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package models

type Table interface {
Validate() (map[string]interface{}, bool)
}
}
Loading

0 comments on commit 91d660b

Please sign in to comment.