2017-07-22 01:14:16 +02:00
package models
2017-07-28 20:43:22 +02:00
import (
"errors"
"net/http"
"github.com/fatih/structs"
)
2017-07-22 01:14:16 +02:00
// Tag model for a torrent vote system
type Tag struct {
TorrentID uint ` gorm:"column:torrent_id" `
UserID uint ` gorm:"column:user_id" `
Tag string ` gorm:"column:tag" `
Type string ` gorm:"column:type" `
Weight float64 ` gorm:"column:weight" `
Accepted bool ` gorm:"column:accepted" `
2017-07-28 20:43:22 +02:00
Total float64 ` gorm:"-" `
}
// Update a tag
func ( ta * Tag ) Update ( ) ( int , error ) {
if ORM . Model ( ta ) . UpdateColumn ( ta . toMap ( ) ) . Error != nil {
return http . StatusInternalServerError , errors . New ( "Tag was not updated" )
}
return http . StatusOK , nil
}
// Delete : delete a tag based on id
func ( ta * Tag ) Delete ( ) ( int , error ) {
2017-07-31 03:15:37 +02:00
if ORM . Where ( "tag = ? AND type = ? AND torrent_id = ? AND user_id = ?" , ta . Tag , ta . Type , ta . TorrentID , ta . UserID ) . Delete ( ta ) . Error != nil {
2017-07-28 20:43:22 +02:00
return http . StatusInternalServerError , errors . New ( "tag_not_deleted" )
}
return http . StatusOK , nil
}
// toMap : convert the model to a map of interface
func ( ta * Tag ) toMap ( ) map [ string ] interface { } {
return structs . Map ( ta )
2017-07-22 01:14:16 +02:00
}
2017-07-30 03:29:09 +02:00
type Tags [ ] Tag
2017-08-01 11:17:25 +02:00
// Contains check if the tag map has the same tag in it (tag value + tag type)
func ( ts Tags ) Contains ( tag Tag ) bool {
for _ , ta := range ts {
2017-07-30 03:29:09 +02:00
if ta . Tag == tag . Tag && ta . Type == tag . Type {
return true
}
}
return false
}
2017-08-01 11:17:25 +02:00
// HasAccepted check if a tag has been accepted in the tags map
func ( ts Tags ) HasAccepted ( ) bool {
for _ , tag := range ts {
if tag . Accepted {
return true
}
}
return false
}