Initial commit
هذا الالتزام موجود في:
31
internal/models/db.go
Normal file
31
internal/models/db.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"gorm.io/driver/sqlite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/logger"
|
||||
)
|
||||
|
||||
var db *gorm.DB
|
||||
|
||||
func Setup(dbpath string) error {
|
||||
var err error
|
||||
|
||||
if db, err = gorm.Open(sqlite.Open(dbpath+"?_fk=true"), &gorm.Config{
|
||||
Logger: logger.Default.LogMode(logger.Silent),
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err = db.AutoMigrate(&User{}, &SSHKey{}, &Gist{}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func CountAll(table interface{}) (int64, error) {
|
||||
var count int64
|
||||
err := db.Model(table).Count(&count).Error
|
||||
return count, err
|
||||
}
|
||||
136
internal/models/gist.go
Normal file
136
internal/models/gist.go
Normal file
@@ -0,0 +1,136 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
type Gist struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Uuid string
|
||||
Title string `validate:"max=50" form:"title"`
|
||||
Preview string
|
||||
PreviewFilename string
|
||||
Description string `validate:"max=150" form:"description"`
|
||||
Private bool `form:"private"`
|
||||
UserID uint
|
||||
User User `validate:"-"`
|
||||
NbFiles int
|
||||
NbLikes int
|
||||
CreatedAt int64
|
||||
UpdatedAt int64
|
||||
|
||||
Likes []User `gorm:"many2many:likes;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
||||
|
||||
Files []File `gorm:"-" validate:"min=1,dive"`
|
||||
}
|
||||
|
||||
type File struct {
|
||||
Filename string `validate:"excludes=\x2f,excludes=\x5c,max=50"`
|
||||
OldFilename string `validate:"excludes=\x2f,excludes=\x5c,max=50"`
|
||||
Content string `validate:"required"`
|
||||
}
|
||||
|
||||
type Commit struct {
|
||||
Hash string
|
||||
Author string
|
||||
Timestamp string
|
||||
Changed string
|
||||
Files []File
|
||||
}
|
||||
|
||||
func GetGist(user string, gistUuid string) (*Gist, error) {
|
||||
gist := new(Gist)
|
||||
err := db.Preload("User").
|
||||
Where("gists.uuid = ? AND users.username like ?", gistUuid, user).
|
||||
Joins("join users on gists.user_id = users.id").
|
||||
First(&gist).Error
|
||||
|
||||
return gist, err
|
||||
}
|
||||
|
||||
func GetGistByID(gistId string) (*Gist, error) {
|
||||
gist := new(Gist)
|
||||
err := db.Preload("User").
|
||||
Where("gists.id = ?", gistId).
|
||||
First(&gist).Error
|
||||
|
||||
return gist, err
|
||||
}
|
||||
|
||||
func GetAllGistsForCurrentUser(currentUserId uint, offset int, sort string, order string) ([]*Gist, error) {
|
||||
var gists []*Gist
|
||||
err := db.Preload("User").
|
||||
Where("gists.private = 0 or gists.user_id = ?", currentUserId).
|
||||
Limit(11).
|
||||
Offset(offset * 10).
|
||||
Order(sort + "_at " + order).
|
||||
Find(&gists).Error
|
||||
|
||||
return gists, err
|
||||
}
|
||||
|
||||
func GetAllGists(offset int) ([]*Gist, error) {
|
||||
var all []*Gist
|
||||
err := db.Preload("User").
|
||||
Limit(11).
|
||||
Offset(offset * 10).
|
||||
Order("id asc").
|
||||
Find(&all).Error
|
||||
|
||||
return all, err
|
||||
}
|
||||
|
||||
func GetAllGistsFromUser(fromUser string, currentUserId uint, offset int, sort string, order string) ([]*Gist, error) {
|
||||
var gists []*Gist
|
||||
err := db.Preload("User").
|
||||
Where("users.username = ? and ((gists.private = 0) or (gists.private = 1 and gists.user_id = ?))", fromUser, currentUserId).
|
||||
Joins("join users on gists.user_id = users.id").
|
||||
Limit(11).
|
||||
Offset(offset * 10).
|
||||
Order("gists." + sort + "_at " + order).
|
||||
Find(&gists).Error
|
||||
|
||||
return gists, err
|
||||
}
|
||||
|
||||
func CreateGist(gist *Gist) error {
|
||||
return db.Create(&gist).Error
|
||||
}
|
||||
|
||||
func UpdateGist(gist *Gist) error {
|
||||
return db.Save(&gist).Error
|
||||
}
|
||||
|
||||
func DeleteGist(gist *Gist) error {
|
||||
return db.Delete(&gist).Error
|
||||
}
|
||||
|
||||
func GistLastActiveNow(gistID uint) error {
|
||||
return db.Model(&Gist{}).
|
||||
Where("id = ?", gistID).
|
||||
Update("updated_at", time.Now().Unix()).Error
|
||||
}
|
||||
|
||||
func AppendUserLike(gist *Gist, user *User) error {
|
||||
db.Model(&gist).Omit("updated_at").Update("nb_likes", gist.NbLikes+1)
|
||||
return db.Model(&gist).Omit("updated_at").Association("Likes").Append(user)
|
||||
}
|
||||
|
||||
func RemoveUserLike(gist *Gist, user *User) error {
|
||||
db.Model(&gist).Omit("updated_at").Update("nb_likes", gist.NbLikes-1)
|
||||
return db.Model(&gist).Omit("updated_at").Association("Likes").Delete(user)
|
||||
}
|
||||
|
||||
func GetUsersLikesForGists(gist *Gist, offset int) ([]*User, error) {
|
||||
var users []*User
|
||||
err := db.Model(&gist).
|
||||
Where("gist_id = ?", gist.ID).
|
||||
Limit(31).
|
||||
Offset(offset * 30).
|
||||
Association("Likes").Find(&users)
|
||||
return users, err
|
||||
}
|
||||
|
||||
func UserCanWrite(user *User, gist *Gist) bool {
|
||||
return !(user == nil) && (gist.UserID == user.ID)
|
||||
}
|
||||
56
internal/models/sshkey.go
Normal file
56
internal/models/sshkey.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package models
|
||||
|
||||
import "time"
|
||||
|
||||
type SSHKey struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Title string `form:"title" validate:"required,max=50"`
|
||||
Content string `form:"content" validate:"required"`
|
||||
SHA string
|
||||
CreatedAt int64
|
||||
LastUsedAt int64
|
||||
UserID uint
|
||||
User User `validate:"-" `
|
||||
}
|
||||
|
||||
func GetSSHKeysByUserID(userId uint) ([]*SSHKey, error) {
|
||||
var sshKeys []*SSHKey
|
||||
err := db.
|
||||
Where("user_id = ?", userId).
|
||||
Order("created_at asc").
|
||||
Find(&sshKeys).Error
|
||||
|
||||
return sshKeys, err
|
||||
}
|
||||
|
||||
func GetSSHKeyByID(sshKeyId uint) (*SSHKey, error) {
|
||||
sshKey := new(SSHKey)
|
||||
err := db.
|
||||
Where("id = ?", sshKeyId).
|
||||
First(&sshKey).Error
|
||||
|
||||
return sshKey, err
|
||||
}
|
||||
|
||||
func GetSSHKeyByContent(sshKeyContent string) (*SSHKey, error) {
|
||||
sshKey := new(SSHKey)
|
||||
err := db.
|
||||
Where("content like ?", sshKeyContent+"%").
|
||||
First(&sshKey).Error
|
||||
|
||||
return sshKey, err
|
||||
}
|
||||
|
||||
func AddSSHKey(sshKey *SSHKey) error {
|
||||
return db.Create(&sshKey).Error
|
||||
}
|
||||
|
||||
func RemoveSSHKey(sshKey *SSHKey) error {
|
||||
return db.Delete(&sshKey).Error
|
||||
}
|
||||
|
||||
func SSHKeyLastUsedNow(sshKeyID uint) error {
|
||||
return db.Model(&SSHKey{}).
|
||||
Where("id = ?", sshKeyID).
|
||||
Update("last_used_at", time.Now().Unix()).Error
|
||||
}
|
||||
77
internal/models/user.go
Normal file
77
internal/models/user.go
Normal file
@@ -0,0 +1,77 @@
|
||||
package models
|
||||
|
||||
type User struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Username string `form:"username" gorm:"uniqueIndex" validate:"required,max=24,alphanum,notreserved"`
|
||||
Password string `form:"password" validate:"required"`
|
||||
IsAdmin bool
|
||||
CreatedAt int64
|
||||
|
||||
Gists []Gist `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UserID"`
|
||||
SSHKeys []SSHKey `gorm:"foreignKey:UserID"`
|
||||
Liked []Gist `gorm:"many2many:likes;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
||||
}
|
||||
|
||||
func DoesUserExists(userName string, count *int64) error {
|
||||
return db.Table("users").
|
||||
Where("username like ?", userName).
|
||||
Count(count).Error
|
||||
}
|
||||
|
||||
func GetAllUsers(offset int) ([]*User, error) {
|
||||
var all []*User
|
||||
err := db.
|
||||
Limit(11).
|
||||
Offset(offset * 10).
|
||||
Order("id asc").
|
||||
Find(&all).Error
|
||||
|
||||
return all, err
|
||||
}
|
||||
|
||||
func GetLoginUser(user *User) error {
|
||||
return db.
|
||||
Where("username like ?", user.Username).
|
||||
First(&user).Error
|
||||
}
|
||||
|
||||
func GetLoginUserById(user *User) error {
|
||||
return db.
|
||||
Where("id = ?", user.ID).
|
||||
First(&user).Error
|
||||
}
|
||||
|
||||
func CreateUser(user *User) error {
|
||||
return db.Create(&user).Error
|
||||
}
|
||||
|
||||
func DeleteUserByID(userid string) error {
|
||||
return db.Delete(&User{}, "id = ?", userid).Error
|
||||
}
|
||||
|
||||
func SetAdminUser(user *User) error {
|
||||
return db.Model(&user).Update("is_admin", true).Error
|
||||
}
|
||||
|
||||
func GetUserBySSHKeyID(sshKeyId uint) (*User, error) {
|
||||
user := new(User)
|
||||
err := db.
|
||||
Preload("SSHKeys").
|
||||
Joins("join ssh_keys on users.id = ssh_keys.user_id").
|
||||
Where("ssh_keys.id = ?", sshKeyId).
|
||||
First(&user).Error
|
||||
|
||||
return user, err
|
||||
}
|
||||
|
||||
func UserHasLikedGist(user *User, gist *Gist) (bool, error) {
|
||||
association := db.Model(&gist).Where("user_id = ?", user.ID).Association("Likes")
|
||||
if association.Error != nil {
|
||||
return false, association.Error
|
||||
}
|
||||
|
||||
if association.Count() == 0 {
|
||||
return false, nil
|
||||
}
|
||||
return true, nil
|
||||
}
|
||||
المرجع في مشكلة جديدة
حظر مستخدم