diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index 08f1f20..7768715 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -38,11 +38,11 @@ jobs: steps: - name: Checkout repository - uses: actions/checkout@v3 + uses: actions/checkout@v4 # Initializes the CodeQL tools for scanning. - name: Initialize CodeQL - uses: github/codeql-action/init@v2 + uses: github/codeql-action/init@v3 with: languages: ${{ matrix.language }} # If you wish to specify custom queries, you can do so here or in a config file. @@ -56,7 +56,7 @@ jobs: # Autobuild attempts to build any compiled languages (C/C++, C#, or Java). # If this step fails, then you should remove it and run the build manually (see below) - name: Autobuild - uses: github/codeql-action/autobuild@v2 + uses: github/codeql-action/autobuild@v3 # ℹī¸ Command-line programs to run using the OS shell. # 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun @@ -69,4 +69,4 @@ jobs: # ./location_of_script_within_repo/buildscript.sh - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v2 + uses: github/codeql-action/analyze@v3 diff --git a/.github/workflows/docker-image-beta.yml b/.github/workflows/docker-image-beta.yml index 574e801..563e0f3 100644 --- a/.github/workflows/docker-image-beta.yml +++ b/.github/workflows/docker-image-beta.yml @@ -12,10 +12,10 @@ jobs: packages: write steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Docker metadata id: meta - uses: docker/metadata-action@v3 + uses: docker/metadata-action@v5 with: images: | aunefyren/poenskelisten @@ -25,17 +25,17 @@ jobs: flavor: | latest=false - name: Set up QEMU - uses: docker/setup-qemu-action@v2 + uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx id: buildx - uses: docker/setup-buildx-action@v2 + uses: docker/setup-buildx-action@v3 - name: Login to Docker Hub - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - name: Login to GHCR - uses: docker/login-action@v2 + uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.repository_owner }} @@ -45,7 +45,7 @@ jobs: - name: Replace version variable run: sed -i 's/{{RELEASE_TAG}}/beta-${{ env.GITHUB_SHA_SHORT }}/g' config/config.go # Replace release variable with the name of this release - name: Build and push - uses: docker/build-push-action@v5.1.0 + uses: docker/build-push-action@v6 with: builder: ${{ steps.buildx.outputs.name }} file: ./Dockerfile diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 991758f..0ad9f25 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -12,7 +12,7 @@ jobs: packages: write steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Docker metadata id: meta uses: docker/metadata-action@v3 diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index 9a9149a..2ce24a8 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -12,12 +12,12 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ '1.19', '1.20', '1.21.x' ] + go-version: [ '1.21', '1.22.x' ] steps: - uses: actions/checkout@v4 - name: Setup Go ${{ matrix.go-version }} - uses: actions/setup-go@v4 + uses: actions/setup-go@v5 with: go-version: ${{ matrix.go-version }} # You can test your matrix by printing the current Go version diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index e9dc228..f1ed6c9 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -17,8 +17,8 @@ jobs: - goarch: arm64, arm goos: windows steps: - - uses: actions/checkout@v3 - - uses: wangyoucao577/go-release-action@v1.40 + - uses: actions/checkout@v4 + - uses: wangyoucao577/go-release-action@v1 with: github_token: ${{ secrets.GIT_HUB_TOKEN }} goos: ${{ matrix.goos }} diff --git a/controllers/group.go b/controllers/group.go index daca691..8467cf3 100644 --- a/controllers/group.go +++ b/controllers/group.go @@ -886,7 +886,6 @@ func APIUpdateGroup(context *gin.Context) { } func ConvertGroupToGroupObject(group models.Group) (groupObject models.GroupUser, err error) { - err = nil groupObject = models.GroupUser{} // Add owner information to group diff --git a/controllers/news.go b/controllers/news.go index 54524f3..c203be9 100644 --- a/controllers/news.go +++ b/controllers/news.go @@ -6,7 +6,6 @@ import ( "aunefyren/poenskelisten/utilities" "log" "net/http" - "strconv" "strings" "github.com/gin-gonic/gin" @@ -33,7 +32,7 @@ func GetNewsPost(context *gin.Context) { var newsID = context.Param("news_id") - newsIDInt, err := strconv.Atoi(newsID) + newsIDUUID, err := uuid.Parse(newsID) if err != nil { log.Println("Failed to parse request. Error: " + err.Error()) context.JSON(http.StatusBadRequest, gin.H{"error": "Failed parse request."}) @@ -42,7 +41,7 @@ func GetNewsPost(context *gin.Context) { } // Get the newspost by id - newsPost, err := database.GetNewsPostByNewsID(newsIDInt) + newsPost, err := database.GetNewsPostByNewsID(newsIDUUID) if err != nil { // If there is an error getting the news, return an internal server error log.Println("Failed to get news post. Error: " + err.Error()) @@ -150,7 +149,7 @@ func DeleteNewsPost(context *gin.Context) { newsID := context.Param("news_id") // Parse news ID as integer - newsIDInt, err := strconv.Atoi(newsID) + newsIDUUID, err := uuid.Parse(newsID) if err != nil { log.Println("Failed to parse request. Error: " + err.Error()) context.JSON(http.StatusBadRequest, gin.H{"error": "Failed to parse request."}) @@ -159,7 +158,7 @@ func DeleteNewsPost(context *gin.Context) { } // Verify that news post exists - _, err = database.GetNewsPostByNewsID(newsIDInt) + _, err = database.GetNewsPostByNewsID(newsIDUUID) if err != nil { // If there is an error getting the news, return an internal server error log.Println("Failed to get news post. Error: " + err.Error()) @@ -169,7 +168,7 @@ func DeleteNewsPost(context *gin.Context) { } // Set the news post to disabled in the database - err = database.DeleteNewsPost(newsIDInt) + err = database.DeleteNewsPost(newsIDUUID) if err != nil { log.Println("Failed to delete news post. Error: " + err.Error()) context.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete news post."}) diff --git a/controllers/user.go b/controllers/user.go index d51017f..714d031 100644 --- a/controllers/user.go +++ b/controllers/user.go @@ -204,7 +204,7 @@ func RegisterUser(context *gin.Context) { } func GetUser(context *gin.Context) { - + var userObject models.User // Create user request var user = context.Param("user_id") @@ -234,8 +234,7 @@ func GetUser(context *gin.Context) { return } - userObject := models.User{} - if userID == user_id_int || (requestingUserObject.Admin != nil && *requestingUserObject.Admin == true) { + if userID == user_id_int || (requestingUserObject.Admin != nil && *requestingUserObject.Admin) { userObject, err = database.GetAllUserInformationAnyState(user_id_int) if err != nil { log.Println("Failed to get user. Error: " + err.Error()) @@ -258,6 +257,7 @@ func GetUser(context *gin.Context) { } func GetUsers(context *gin.Context) { + var users []models.User // Get user ID userID, err := middlewares.GetAuthUsername(context.GetHeader("Authorization")) @@ -276,10 +276,8 @@ func GetUsers(context *gin.Context) { return } - users := []models.User{} - includeDisabled, okay := context.GetQuery("includeDisabled") - if okay && strings.ToLower(includeDisabled) == "true" && requestingUserObject.Admin != nil && *requestingUserObject.Admin == true { + if okay && strings.ToLower(includeDisabled) == "true" && requestingUserObject.Admin != nil && *requestingUserObject.Admin { users, err = database.GetAllUsers() if err != nil { log.Println("Failed to get all users. Error: " + err.Error()) diff --git a/controllers/wishlist.go b/controllers/wishlist.go index 676a54d..e0786f0 100644 --- a/controllers/wishlist.go +++ b/controllers/wishlist.go @@ -158,7 +158,7 @@ func RegisterWishlist(context *gin.Context) { var wishlists_with_users []models.WishlistUser // If a group was referenced, create the wishlist membership - if group_membership == true { + if group_membership { var wishlistmembershipdb models.WishlistMembership wishlistmembershipdb.GroupID = group_id wishlistmembershipdb.WishlistID = wishlistdb.ID @@ -295,9 +295,6 @@ func DeleteWishlist(context *gin.Context) { } func GetWishlistObjectsFromGroup(group_id uuid.UUID, RequestUserID uuid.UUID) (wishlistObjects []models.WishlistUser, err error) { - err = nil - wishlistObjects = []models.WishlistUser{} - wishlists, err := database.GetWishlistsFromGroup(group_id) if err != nil { return []models.WishlistUser{}, err @@ -379,9 +376,6 @@ func GetWishlist(context *gin.Context) { } func GetWishlistObject(WishlistID uuid.UUID, RequestUserID uuid.UUID) (wishlistObject models.WishlistUser, err error) { - err = nil - wishlistObject = models.WishlistUser{} - wishlist, err := database.GetWishlist(WishlistID) if err != nil { log.Println("Failed to get wishlist '" + WishlistID.String() + "' from DB. Returning. Error: " + err.Error()) @@ -460,7 +454,6 @@ func GetWishlists(context *gin.Context) { } func GetWishlistObjects(UserID uuid.UUID) (wishlistObjects []models.WishlistUser, err error) { - err = nil wishlistObjects = []models.WishlistUser{} wishlists, err := database.GetOwnedWishlists(UserID) @@ -475,9 +468,7 @@ func GetWishlistObjects(UserID uuid.UUID) (wishlistObjects []models.WishlistUser return wishlistObjects, errors.New("Failed to get collaboration wishlists for user '" + UserID.String() + "'.") } - for _, wishlistThroughCollab := range wishlistsThroughCollab { - wishlists = append(wishlists, wishlistThroughCollab) - } + wishlists = append(wishlists, wishlistsThroughCollab...) wishlistObjects, err = ConvertWishlistsToWishlistObjects(wishlists, &UserID) if err != nil { @@ -895,7 +886,6 @@ func APIUpdateWishlist(context *gin.Context) { } func ConvertWishlistCollaberatorToWishlistCollaberatorObject(wishlistCollab models.WishlistCollaborator) (wishlistCollabObject models.WishlistCollaboratorObject, err error) { - err = nil wishlistCollabObject = models.WishlistCollaboratorObject{} userObject, err := database.GetUserInformation(wishlistCollab.UserID) @@ -935,7 +925,6 @@ func ConvertWishlistCollaberatorsToWishlistCollaberatorObjects(wishlistCollabs [ } func ConvertWishlistToWishlistObject(wishlist models.Wishlist, RequestUserID *uuid.UUID) (wishlistObject models.WishlistUser, err error) { - err = nil wishlistObject = models.WishlistUser{} groups, err := database.GetGroupMembersFromWishlist(wishlist.ID) diff --git a/database/client.go b/database/client.go index 65a7177..c881c63 100644 --- a/database/client.go +++ b/database/client.go @@ -2,6 +2,7 @@ package database import ( "aunefyren/poenskelisten/models" + "aunefyren/poenskelisten/utilities" "database/sql" "errors" "fmt" @@ -97,16 +98,21 @@ func CreateTable(dbUsername string, dbPassword string, dbIP string, dbPort int, } func Migrate() { - Instance.AutoMigrate(&models.User{}) - Instance.AutoMigrate(&models.Invite{}) - Instance.AutoMigrate(&models.Group{}) - Instance.AutoMigrate(&models.GroupMembership{}) - Instance.AutoMigrate(&models.Wishlist{}) - Instance.AutoMigrate(&models.WishlistMembership{}) - Instance.AutoMigrate(&models.WishlistCollaborator{}) - Instance.AutoMigrate(&models.Wish{}) - Instance.AutoMigrate(&models.WishClaim{}) - Instance.AutoMigrate(&models.News{}) + errUser := Instance.AutoMigrate(&models.User{}) + errInvite := Instance.AutoMigrate(&models.Invite{}) + errGroup := Instance.AutoMigrate(&models.Group{}) + errGroupMemberhip := Instance.AutoMigrate(&models.GroupMembership{}) + errWishlist := Instance.AutoMigrate(&models.Wishlist{}) + errWishlistMembership := Instance.AutoMigrate(&models.WishlistMembership{}) + errWishlistCollaborator := Instance.AutoMigrate(&models.WishlistCollaborator{}) + errWish := Instance.AutoMigrate(&models.Wish{}) + errWishClaim := Instance.AutoMigrate(&models.WishClaim{}) + errNews := Instance.AutoMigrate(&models.News{}) + + err := errors.Join(errUser, errInvite, errGroup, errGroupMemberhip, errWishlist, errWishlistMembership, errWishlistCollaborator, errWish, errWishClaim, errNews) + if err != nil { + panic(err) + } log.Println("Database Migration Completed!") } @@ -133,7 +139,7 @@ func GenrateRandomVerificationCodeForuser(userID uuid.UUID) (string, error) { verificationCode := strings.ToUpper(randomString) var user models.User - userrecord := Instance.Model(user).Where("enabled = ?", true).Where("ID = ?", userID).Update("verification_code", verificationCode) + userrecord := Instance.Model(user).Where(&models.User{Enabled: true}).Where(&models.GormModel{ID: userID}).Update("verification_code", verificationCode) if userrecord.Error != nil { return "", userrecord.Error } @@ -148,7 +154,7 @@ func GenrateRandomVerificationCodeForuser(userID uuid.UUID) (string, error) { // Verify e-mail is not in use func VerifyUniqueUserEmail(providedEmail string) (bool, error) { var user models.User - userrecords := Instance.Where("enabled = ?", true).Where("email = ?", providedEmail).Find(&user) + userrecords := Instance.Where(&models.User{Enabled: true, Email: providedEmail}).Find(&user) if userrecords.Error != nil { return false, userrecords.Error } @@ -161,7 +167,7 @@ func VerifyUniqueUserEmail(providedEmail string) (bool, error) { // Verify if user has a verification code set func VerifyUserHasVerfificationCode(userID uuid.UUID) (bool, error) { var user models.User - userrecords := Instance.Where("enabled = ?", true).Where("ID = ?", userID).Find(&user) + userrecords := Instance.Where(&models.User{Enabled: true}).Where(&models.GormModel{ID: userID}).Find(&user) if userrecords.Error != nil { return false, userrecords.Error } @@ -178,11 +184,8 @@ func VerifyUserHasVerfificationCode(userID uuid.UUID) (bool, error) { // Verify if user has a verification code set func VerifyUserVerfificationCodeMatches(userID uuid.UUID, verificationCode string) (bool, error) { - var user models.User - - userrecords := Instance.Where("enabled = ?", true).Where("ID = ?", userID).Where("verification_code = ?", verificationCode).Find(&user) - + userrecords := Instance.Where(&models.User{Enabled: true, VerificationCode: verificationCode}).Where(&models.GormModel{ID: userID}).Find(&user) if userrecords.Error != nil { return false, userrecords.Error } @@ -197,9 +200,8 @@ func VerifyUserVerfificationCodeMatches(userID uuid.UUID, verificationCode strin // Verify if user is verified func VerifyUserIsVerified(userID uuid.UUID) (bool, error) { - var user models.User - userrecords := Instance.Where("id = ?", userID).Find(&user) + userrecords := Instance.Where(&models.GormModel{ID: userID}).Find(&user) if userrecords.Error != nil { return false, userrecords.Error } @@ -213,7 +215,7 @@ func VerifyUserIsVerified(userID uuid.UUID) (bool, error) { // Verify unsued invite code exists func VerifyUnusedUserInviteCode(providedCode string) (bool, error) { var invitestruct models.Invite - inviterecords := Instance.Where("enabled = ?", true).Where("used = ?", false).Where("code = ?", providedCode).Find(&invitestruct) + inviterecords := Instance.Where(&models.Invite{Used: false, Code: providedCode, Enabled: &utilities.DBTrue}).Find(&invitestruct) if inviterecords.Error != nil { return false, inviterecords.Error } @@ -227,7 +229,7 @@ func VerifyUnusedUserInviteCode(providedCode string) (bool, error) { func SetUsedUserInviteCode(providedCode string, userIDClaimer uuid.UUID) error { var invitestruct models.Invite - inviterecords := Instance.Model(invitestruct).Where("code = ?", providedCode).Update("used", true) + inviterecords := Instance.Model(invitestruct).Where(&models.Invite{Code: providedCode}).Update("used", true) if inviterecords.Error != nil { return inviterecords.Error } @@ -235,7 +237,7 @@ func SetUsedUserInviteCode(providedCode string, userIDClaimer uuid.UUID) error { return errors.New("Code not changed in database.") } - inviterecords = Instance.Model(invitestruct).Where("code = ?", providedCode).Update("recipient_id", userIDClaimer) + inviterecords = Instance.Model(invitestruct).Where(&models.Invite{Code: providedCode}).Update("recipient_id", userIDClaimer) if inviterecords.Error != nil { return inviterecords.Error } @@ -250,15 +252,8 @@ func SetUsedUserInviteCode(providedCode string, userIDClaimer uuid.UUID) error { func SetUserVerification(userID uuid.UUID, verified bool) error { var user models.User - var verInt int - - if verified { - verInt = 1 - } else { - verInt = 0 - } - userrecords := Instance.Model(user).Where("`users`.enabled = ?", 1).Where("`users`.ID = ?", userID).Update("verified", verInt) + userrecords := Instance.Model(user).Where(models.GormModel{ID: userID}).Where(&models.User{Enabled: true}).Update("verified", verified) if userrecords.Error != nil { return userrecords.Error } @@ -274,7 +269,7 @@ func UpdateUserValuesByUserID(userID uuid.UUID, email string, password string) e var user models.User - userrecords := Instance.Model(user).Where("`users`.enabled = ?", 1).Where("`users`.ID = ?", userID).Update("email", email) + userrecords := Instance.Model(user).Where(&models.User{Enabled: true}).Where(&models.GormModel{ID: userID}).Update("email", email) if userrecords.Error != nil { return userrecords.Error } @@ -282,7 +277,7 @@ func UpdateUserValuesByUserID(userID uuid.UUID, email string, password string) e return errors.New("Email not changed in database.") } - userrecords = Instance.Model(user).Where("`users`.enabled = ?", 1).Where("`users`.ID = ?", userID).Update("password", password) + userrecords = Instance.Model(user).Where(&models.User{Enabled: true}).Where(&models.GormModel{ID: userID}).Update("password", password) if userrecords.Error != nil { return userrecords.Error } @@ -296,7 +291,7 @@ func UpdateUserValuesByUserID(userID uuid.UUID, email string, password string) e // Set group to disabled func DeleteGroup(GroupID uuid.UUID) error { var group models.Group - grouprecords := Instance.Model(group).Where("`groups`.ID= ?", GroupID).Update("enabled", 0) + grouprecords := Instance.Model(group).Where(&models.GormModel{ID: GroupID}).Update("enabled", false) if grouprecords.Error != nil { return grouprecords.Error } @@ -309,7 +304,7 @@ func DeleteGroup(GroupID uuid.UUID) error { // Set group membership to disabled func DeleteGroupMembership(GroupMembershipID uuid.UUID) error { var groupmembership models.GroupMembership - grouprecords := Instance.Model(groupmembership).Where("`group_memberships`.ID= ?", GroupMembershipID).Update("enabled", 0) + grouprecords := Instance.Model(groupmembership).Where(&models.GormModel{ID: GroupMembershipID}).Update("enabled", false) if grouprecords.Error != nil { return grouprecords.Error } @@ -322,7 +317,7 @@ func DeleteGroupMembership(GroupMembershipID uuid.UUID) error { // Set wishlist to disabled func DeleteWishlist(WishlistID uuid.UUID) error { var wishlist models.Wishlist - wishlistrecords := Instance.Model(wishlist).Where("`wishlists`.ID= ?", WishlistID).Update("enabled", 0) + wishlistrecords := Instance.Model(wishlist).Where(&models.GormModel{ID: WishlistID}).Update("enabled", false) if wishlistrecords.Error != nil { return wishlistrecords.Error } @@ -335,7 +330,7 @@ func DeleteWishlist(WishlistID uuid.UUID) error { // Set wishlist membership to disabled func DeleteWishlistMembership(WishlistMembershipID uuid.UUID) error { var wishlistmembership models.WishlistMembership - wishlistmembershiprecords := Instance.Model(wishlistmembership).Where("`wishlist_memberships`.ID= ?", WishlistMembershipID).Update("enabled", 0) + wishlistmembershiprecords := Instance.Model(wishlistmembership).Where(&models.GormModel{ID: WishlistMembershipID}).Update("enabled", false) if wishlistmembershiprecords.Error != nil { return wishlistmembershiprecords.Error } @@ -350,7 +345,11 @@ func GetUserMembersFromGroup(GroupID uuid.UUID) ([]models.User, error) { var users []models.User var group_memberships []models.GroupMembership - membershiprecords := Instance.Where("`group_memberships`.enabled = ?", 1).Joins("JOIN `groups` on `group_memberships`.group = `groups`.id").Where("`groups`.enabled = ?", 1).Where("`groups`.id = ?", GroupID).Joins("JOIN `users` on `group_memberships`.group_id = `users`.id").Where("`users`.enabled = ?", 1).Find(&group_memberships) + membershiprecords := Instance. + Where(&models.GroupMembership{Enabled: true}). + Joins("JOIN groups ON group_memberships.group = groups.id", Instance.Where(&models.Group{Enabled: true}).Where(&models.GormModel{ID: GroupID})). + Joins("JOIN users ON group_memberships.group_id = users.id", Instance.Where(&models.User{Enabled: true})). + Find(&group_memberships) if membershiprecords.Error != nil { return []models.User{}, membershiprecords.Error } diff --git a/database/group.go b/database/group.go index c3a3d2b..4b5f5df 100644 --- a/database/group.go +++ b/database/group.go @@ -3,7 +3,6 @@ package database import ( "aunefyren/poenskelisten/models" "errors" - "github.com/google/uuid" ) @@ -11,7 +10,7 @@ func VerifyGroupExistsByNameForUser(groupName string, groupOwnerID uuid.UUID) (b var groupStruct models.Group - groupRecords := Instance.Where("`groups`.enabled = ?", 1).Where("`groups`.name = ?", groupName).Where("`groups`.Owner = ?", groupOwnerID).Find(&groupStruct) + groupRecords := Instance.Where(&models.Group{Enabled: true, Name: groupName, OwnerID: groupOwnerID}).Find(&groupStruct) if groupRecords.Error != nil { return false, models.Group{}, groupRecords.Error @@ -25,7 +24,7 @@ func VerifyGroupExistsByNameForUser(groupName string, groupOwnerID uuid.UUID) (b // Get group by Group ID func GetGroupInformation(GroupID uuid.UUID) (models.Group, error) { var group models.Group - grouprecord := Instance.Where("`groups`.enabled = ?", 1).Where("`groups`.id = ?", GroupID).Find(&group) + grouprecord := Instance.Where(&models.Group{Enabled: true}).Where(&models.GormModel{ID: GroupID}).Find(&group) if grouprecord.Error != nil { return models.Group{}, grouprecord.Error } else if grouprecord.RowsAffected != 1 { @@ -40,7 +39,7 @@ func UpdateGroupValuesByID(groupID uuid.UUID, groupName string, groupDesc string var group models.Group - groupRecord := Instance.Model(group).Where("`groups`.enabled = ?", 1).Where("`groups`.id = ?", groupID).Update("name", groupName) + groupRecord := Instance.Model(group).Where(&models.Group{Enabled: true}).Where(&models.GormModel{ID: groupID}).Update("name", groupName) if groupRecord.Error != nil { return groupRecord.Error } @@ -48,7 +47,7 @@ func UpdateGroupValuesByID(groupID uuid.UUID, groupName string, groupDesc string return errors.New("Name not changed in database.") } - groupRecord = Instance.Model(group).Where("`groups`.enabled = ?", 1).Where("`groups`.id = ?", groupID).Update("description", groupDesc) + groupRecord = Instance.Model(group).Where(&models.Group{Enabled: true}).Where(&models.GormModel{ID: groupID}).Update("description", groupDesc) if groupRecord.Error != nil { return groupRecord.Error } @@ -64,10 +63,7 @@ func UpdateGroupValuesByID(groupID uuid.UUID, groupName string, groupDesc string func VerifyUserOwnershipToGroup(UserID uuid.UUID, GroupID uuid.UUID) (bool, error) { var group models.Group - grouprecord := Instance.Where("`groups`.enabled = ?", 1). - Where("`groups`.id = ?", GroupID). - Where("`groups`.owner_id = ?", UserID). - Find(&group) + grouprecord := Instance.Where(&models.Group{Enabled: true, OwnerID: UserID}).Where(&models.GormModel{ID: GroupID}).Find(&group) if grouprecord.Error != nil { return false, grouprecord.Error @@ -84,15 +80,10 @@ func GetGroupMembersFromWishlist(WishlistID uuid.UUID) ([]models.Group, error) { var groups []models.Group groupsrecords := Instance. - Where("`groups`.enabled = ?", 1). - Joins("JOIN `group_memberships` on `groups`.id = `group_memberships`.group_id"). - Where("`group_memberships`.enabled = ?", 1). - Joins("JOIN `users` on `group_memberships`.member_id = `users`.id"). - Where("`users`.enabled = ?", 1). - Joins("JOIN `wishlist_memberships` on `groups`.id = `wishlist_memberships`.group_id"). - Where("`wishlist_memberships`.enabled = ?", 1). - Where("`wishlist_memberships`.wishlist_id = ?", WishlistID). - Group("groups.ID"). + Where(&models.Group{Enabled: true}). + Joins("JOIN group_memberships ON groups.id = group_memberships.group_id", Instance.Where(&models.GroupMembership{Enabled: true})). + Joins("JOIN users ON group_memberships.member_id = users.id", Instance.Where(&models.User{Enabled: true})). + Joins("JOIN wishlist_memberships ON groups.id = wishlist_memberships.group_id", Instance.Where(&models.WishlistMembership{WishlistID: WishlistID, Enabled: true})). Find(&groups) if groupsrecords.Error != nil { @@ -110,10 +101,9 @@ func GetGroupsAUserIsAMemberOf(UserID uuid.UUID) ([]models.Group, error) { var groups []models.Group // Retrieve groups that the user is a member of - groupRecords := Instance.Where("`groups`.enabled = ?", 1). - Joins("JOIN group_memberships on group_memberships.group_id = groups.id"). - Where("`group_memberships`.enabled = ?", 1). - Where("`group_memberships`.member_id = ?", UserID). + groupRecords := Instance.Distinct(). + Where(&models.Group{Enabled: true}). + Joins("JOIN group_memberships ON group_memberships.group_id = groups.id", Instance.Where(&models.GroupMembership{Enabled: true, MemberID: UserID})). Find(&groups) if groupRecords.Error != nil { @@ -135,10 +125,9 @@ func GetGroupMembershipsFromGroup(GroupID uuid.UUID) ([]models.GroupMembership, var groupMemberships []models.GroupMembership groupmembershipRecords := Instance. - Where("`group_memberships`.enabled = ?", 1). - Where("`group_memberships`.group_id = ?", GroupID). - Joins("JOIN `users` on `group_memberships`.member_id = `users`.id"). - Where("`users`.enabled = ?", 1).Find(&groupMemberships) + Where(&models.GroupMembership{Enabled: true, GroupID: GroupID}). + Joins("JOIN users ON group_memberships.member_id = users.id", Instance.Where(&models.User{Enabled: true})). + Find(&groupMemberships) if groupmembershipRecords.Error != nil { return []models.GroupMembership{}, groupmembershipRecords.Error @@ -158,10 +147,7 @@ func GetGroupMembershipsFromGroup(GroupID uuid.UUID) ([]models.GroupMembership, func VerifyIfGroupWithSameNameAndOwnerDoesNotExist(GroupName string, GroupOwnerID uuid.UUID) (bool, error) { var group = []models.Group{} - groupRecords := Instance.Where("`groups`.enabled = ?", 1). - Where("`groups`.name = ?", GroupName). - Where("`groups`.owner_id = ?", GroupOwnerID). - Find(&group) + groupRecords := Instance.Where(&models.Group{Enabled: true, Name: GroupName, OwnerID: GroupOwnerID}).Find(&group) if groupRecords.Error != nil { return true, groupRecords.Error @@ -174,7 +160,7 @@ func VerifyIfGroupWithSameNameAndOwnerDoesNotExist(GroupName string, GroupOwnerI // Verify if a user ID is a member of a group func VerifyUserMembershipToGroup(UserID uuid.UUID, GroupID uuid.UUID) (bool, error) { var groupmembership models.GroupMembership - groupmembershiprecord := Instance.Where("`group_memberships`.enabled = ?", 1).Where("`group_memberships`.group_id = ?", GroupID).Where("`group_memberships`.member_id = ?", UserID).Find(&groupmembership) + groupmembershiprecord := Instance.Where(&models.GroupMembership{Enabled: true, GroupID: GroupID, MemberID: UserID}).Find(&groupmembership) if groupmembershiprecord.Error != nil { return false, groupmembershiprecord.Error } else if groupmembershiprecord.RowsAffected != 1 { @@ -187,7 +173,7 @@ func VerifyUserMembershipToGroup(UserID uuid.UUID, GroupID uuid.UUID) (bool, err func VerifyGroupMembershipToWishlist(WishlistID uuid.UUID, GroupID uuid.UUID) (bool, error) { var wishlistmembership models.WishlistMembership - wishlistmembershipprecord := Instance.Where("`wishlist_memberships`.enabled = ?", 1).Where("`wishlist_memberships`.wishlist_id = ?", WishlistID).Where("`wishlist_memberships`.group_id = ?", GroupID).Find(&wishlistmembership) + wishlistmembershipprecord := Instance.Where(&models.WishlistMembership{Enabled: true, WishlistID: WishlistID, GroupID: GroupID}).Find(&wishlistmembership) if wishlistmembershipprecord.Error != nil { return false, wishlistmembershipprecord.Error } else if wishlistmembershipprecord.RowsAffected != 1 { @@ -202,11 +188,8 @@ func GetGroupUsingGroupIDAndMembershipUsingUserID(UserID uuid.UUID, GroupID uuid var group = models.Group{} groupRecord := Instance. - Where("`groups`.enabled = ?", 1). - Joins("JOIN group_memberships on group_memberships.group_id = groups.id"). - Where("`group_memberships`.enabled = ?", 1). - Where("`group_memberships`.member_id = ?", UserID). - Where("`group_memberships`.group_id = ?", GroupID). + Where(&models.Group{Enabled: true}). + Joins("JOIN group_memberships ON group_memberships.group_id = groups.id", Instance.Where(&models.GroupMembership{Enabled: true, MemberID: UserID, GroupID: GroupID})). Find(&group) if groupRecord.Error != nil { @@ -221,7 +204,7 @@ func GetGroupUsingGroupIDAndMembershipUsingUserID(UserID uuid.UUID, GroupID uuid func GetGroupUsingGroupIDAndUserIDAsOwner(UserID uuid.UUID, GroupID uuid.UUID) (models.Group, error) { var group = models.Group{} - groupRecord := Instance.Where("`groups`.enabled = ?", 1).Where("`groups`.id = ?", GroupID).Where("`groups`.owner_id = ?", UserID).Find(&group) + groupRecord := Instance.Where(&models.Group{Enabled: true, OwnerID: UserID}).Where(&models.GormModel{ID: GroupID}).Find(&group) if groupRecord.Error != nil { return group, groupRecord.Error } else if groupRecord.RowsAffected != 1 { @@ -236,7 +219,7 @@ func GetGroupMembershipByGroupIDAndMemberID(GroupID uuid.UUID, MemberID uuid.UUI groupmembership = models.GroupMembership{} err = nil - groupMembershipRecord := Instance.Where("`group_memberships`.enabled = ?", 1).Where("`group_memberships`.group_id = ?", GroupID).Where("`group_memberships`.member_id = ?", MemberID).Find(&groupmembership) + groupMembershipRecord := Instance.Where(&models.GroupMembership{Enabled: true, GroupID: GroupID, MemberID: MemberID}).Find(&groupmembership) if groupMembershipRecord.Error != nil { return groupmembership, groupMembershipRecord.Error } else if groupMembershipRecord.RowsAffected != 1 { diff --git a/database/invite.go b/database/invite.go index f3186da..ae9da58 100644 --- a/database/invite.go +++ b/database/invite.go @@ -2,6 +2,7 @@ package database import ( "aunefyren/poenskelisten/models" + "aunefyren/poenskelisten/utilities" "errors" "github.com/google/uuid" @@ -10,7 +11,8 @@ import ( // Get invites in database that have not been disabled func GetAllEnabledInvites() ([]models.Invite, error) { var invitestruct []models.Invite - inviterecords := Instance.Where("`invites`.enabled = ?", 1).Find(&invitestruct) + var LolTrue = true + inviterecords := Instance.Where(&models.Invite{Enabled: &LolTrue}).Find(&invitestruct) if inviterecords.Error != nil { return []models.Invite{}, inviterecords.Error } @@ -23,7 +25,7 @@ func GetAllEnabledInvites() ([]models.Invite, error) { // Get invite using ID func GetInviteByID(inviteID uuid.UUID) (models.Invite, error) { var invitestruct models.Invite - inviterecords := Instance.Where("`invites`.enabled = ?", 1).Where("`invites`.id = ?", inviteID).Find(&invitestruct) + inviterecords := Instance.Where(&models.Invite{Enabled: &utilities.DBTrue}).Where(&models.GormModel{ID: inviteID}).Find(&invitestruct) if inviterecords.Error != nil { return models.Invite{}, inviterecords.Error } @@ -36,7 +38,7 @@ func GetInviteByID(inviteID uuid.UUID) (models.Invite, error) { // Set invite to disabled by ID func DeleteInviteByID(inviteID uuid.UUID) error { var invitestruct models.Invite - inviterecords := Instance.Model(invitestruct).Where("`invites`.id = ?", inviteID).Update("enabled", 0) + inviterecords := Instance.Model(invitestruct).Where(&models.GormModel{ID: inviteID}).Update("enabled", false) if inviterecords.Error != nil { return inviterecords.Error } diff --git a/database/news.go b/database/news.go index e9ec7c7..89ebd65 100644 --- a/database/news.go +++ b/database/news.go @@ -3,12 +3,13 @@ package database import ( "aunefyren/poenskelisten/models" "errors" + "github.com/google/uuid" ) // Set news post to disabled -func DeleteNewsPost(newsID int) error { +func DeleteNewsPost(newsID uuid.UUID) error { var news models.News - newsRecords := Instance.Model(news).Where("`news`.ID= ?", newsID).Update("enabled", 0) + newsRecords := Instance.Model(news).Where(&models.GormModel{ID: newsID}).Update("enabled", 0) if newsRecords.Error != nil { return newsRecords.Error } @@ -22,7 +23,7 @@ func GetNewsPosts() ([]models.News, error) { var newsPosts []models.News - newsPostsRecords := Instance.Order("date desc").Where("`news`.enabled = ?", 1).Find(&newsPosts) + newsPostsRecords := Instance.Where(&models.News{Enabled: true}).Order("date desc").Find(&newsPosts) if newsPostsRecords.Error != nil { return []models.News{}, newsPostsRecords.Error @@ -38,11 +39,11 @@ func GetNewsPosts() ([]models.News, error) { } -func GetNewsPostByNewsID(newsID int) (models.News, error) { +func GetNewsPostByNewsID(newsID uuid.UUID) (models.News, error) { var newsPost models.News - newsPostRecords := Instance.Where("`news`.enabled = ?", 1).Where("`news`.id = ?", newsID).Find(&newsPost) + newsPostRecords := Instance.Where(&models.GormModel{ID: newsID}).Where(&models.News{Enabled: true}).Find(&newsPost) if newsPostRecords.Error != nil { return models.News{}, newsPostRecords.Error diff --git a/database/user.go b/database/user.go index 358a181..91597a0 100644 --- a/database/user.go +++ b/database/user.go @@ -13,7 +13,7 @@ import ( // Get redacted user information based on User ID for enabled users func GetUserInformation(UserID uuid.UUID) (models.User, error) { var user models.User - userrecord := Instance.Where("`users`.enabled = ?", 1).Where("`users`.id = ?", UserID).Find(&user) + userrecord := Instance.Where(&models.GormModel{ID: UserID}).Where(&models.User{Enabled: true}).Find(&user) if userrecord.Error != nil { return models.User{}, userrecord.Error } else if userrecord.RowsAffected != 1 { @@ -29,7 +29,7 @@ func GetUserInformation(UserID uuid.UUID) (models.User, error) { // Get redacted user information based on User ID for all users func GetUserInformationAnyState(UserID uuid.UUID) (models.User, error) { var user models.User - userrecord := Instance.Where("`users`.id = ?", UserID).Find(&user) + userrecord := Instance.Where(&models.GormModel{ID: UserID}).Find(&user) if userrecord.Error != nil { return models.User{}, userrecord.Error } else if userrecord.RowsAffected != 1 { @@ -45,7 +45,7 @@ func GetUserInformationAnyState(UserID uuid.UUID) (models.User, error) { // Get ALL user information for enabled users (non-redacted) func GetAllUserInformation(UserID uuid.UUID) (models.User, error) { var user models.User - userrecord := Instance.Where("enabled = ?", true).Where("id = ?", UserID).Find(&user) + userrecord := Instance.Where(&models.GormModel{ID: UserID}).Where(&models.User{Enabled: true}).Find(&user) if userrecord.Error != nil { return models.User{}, userrecord.Error } else if userrecord.RowsAffected != 1 { @@ -58,7 +58,7 @@ func GetAllUserInformation(UserID uuid.UUID) (models.User, error) { // Get ALL user information for ALL users (non-redacted) func GetAllUserInformationAnyState(UserID uuid.UUID) (models.User, error) { var user models.User - userrecord := Instance.Where("id = ?", UserID).Find(&user) + userrecord := Instance.Where(&models.GormModel{ID: UserID}).Find(&user) if userrecord.Error != nil { return models.User{}, userrecord.Error } else if userrecord.RowsAffected != 1 { @@ -71,7 +71,7 @@ func GetAllUserInformationAnyState(UserID uuid.UUID) (models.User, error) { // Get redacted user information using email func GetUserInformationByEmail(email string) (models.User, error) { var user models.User - userrecord := Instance.Where("`users`.enabled = ?", 1).Where("`users`.email = ?", email).Find(&user) + userrecord := Instance.Where(&models.User{Enabled: true, Email: email}).Find(&user) if userrecord.Error != nil { return models.User{}, userrecord.Error } else if userrecord.RowsAffected != 1 { @@ -88,7 +88,7 @@ func GetUserInformationByEmail(email string) (models.User, error) { func GetAllUserInformationByEmail(email string) (models.User, error) { var user models.User - userrecord := Instance.Where("`users`.enabled = ?", 1).Where("`users`.email = ?", email).Find(&user) + userrecord := Instance.Where(&models.User{Enabled: true, Email: email}).Find(&user) if userrecord.Error != nil { return models.User{}, userrecord.Error } else if userrecord.RowsAffected != 1 { @@ -107,7 +107,7 @@ func GenerateRandomResetCodeForUser(userID uuid.UUID) (string, error) { expirationDate := time.Now().AddDate(0, 0, 7) var user models.User - userrecord := Instance.Model(user).Where("`users`.enabled = ?", 1).Where("`users`.ID = ?", userID).Update("reset_code", resetCode) + userrecord := Instance.Model(user).Where(&models.GormModel{ID: userID}).Where(&models.User{Enabled: true}).Update("reset_code", resetCode) if userrecord.Error != nil { return "", userrecord.Error } @@ -115,7 +115,7 @@ func GenerateRandomResetCodeForUser(userID uuid.UUID) (string, error) { return "", errors.New("Reset code not changed in database.") } - userrecord = Instance.Model(user).Where("`users`.enabled = ?", 1).Where("`users`.ID = ?", userID).Update("reset_expiration", expirationDate) + userrecord = Instance.Model(user).Where(&models.GormModel{ID: userID}).Where(&models.User{Enabled: true}).Update("reset_expiration", expirationDate) if userrecord.Error != nil { return "", userrecord.Error } @@ -130,7 +130,7 @@ func GenerateRandomResetCodeForUser(userID uuid.UUID) (string, error) { // Retrieve ALL user information using the reset code on the user object func GetAllUserInformationByResetCode(resetCode string) (models.User, error) { var user models.User - userrecord := Instance.Where("`users`.enabled = ?", 1).Where("`users`.reset_code = ?", resetCode).Find(&user) + userrecord := Instance.Where(&models.User{Enabled: true, ResetCode: resetCode}).Find(&user) if userrecord.Error != nil { return models.User{}, userrecord.Error } else if userrecord.RowsAffected != 1 { @@ -145,7 +145,7 @@ func GetAmountOfEnabledUsers() (int, error) { var users []models.User - userRecords := Instance.Where("enabled = ?", true).Find(&users) + userRecords := Instance.Where(&models.User{Enabled: true}).Find(&users) if userRecords.Error != nil { return 0, userRecords.Error } @@ -171,7 +171,7 @@ func GetEnabledUsers() (usersRedacted []models.User, err error) { usersRedacted = []models.User{} err = nil - userrecord := Instance.Where("`users`.enabled = ?", 1).Find(&users) + userrecord := Instance.Where(&models.User{Enabled: true}).Find(&users) if userrecord.Error != nil { return usersRedacted, userrecord.Error } diff --git a/database/wish.go b/database/wish.go index 11ef08a..3e386e7 100644 --- a/database/wish.go +++ b/database/wish.go @@ -10,7 +10,7 @@ import ( // Get wishlist id from wish id func GetWishlistFromWish(WishID uuid.UUID) (bool, uuid.UUID, error) { var wish models.Wish - wishrecord := Instance.Where("`wishes`.enabled = ?", 1).Where("`wishes`.id = ?", WishID).Find(&wish) + wishrecord := Instance.Where(&models.Wish{Enabled: true}).Where(&models.GormModel{ID: WishID}).Find(&wish) if wishrecord.Error != nil { return false, uuid.UUID{}, wishrecord.Error } else if wishrecord.RowsAffected != 1 { @@ -25,7 +25,7 @@ func GetWishByWishID(wishID uuid.UUID) (bool, models.Wish, error) { var wish models.Wish - wishRecord := Instance.Where("`wishes`.enabled = ?", 1).Where("`wishes`.id = ?", wishID).Find(&wish) + wishRecord := Instance.Where(&models.Wish{Enabled: true}).Where(&models.GormModel{ID: wishID}).Find(&wish) if wishRecord.Error != nil { return false, models.Wish{}, wishRecord.Error @@ -37,32 +37,32 @@ func GetWishByWishID(wishID uuid.UUID) (bool, models.Wish, error) { } -func UpdateWishValuesInDatabase(wishID uuid.UUID, wishName string, wishNote string, wishURL string, wishprice float64) error { +func UpdateWishValuesInDatabase(wishID uuid.UUID, wishName string, wishNote string, wishURL string, wishPrice float64) error { var wish models.Wish - wishRecord := Instance.Model(wish).Where("`wishes`.enabled = ?", 1).Where("`wishes`.ID = ?", wishID).Update("name", wishName) + wishRecord := Instance.Model(wish).Where(&models.Wish{Enabled: true}).Where(&models.GormModel{ID: wishID}).Update("name", wishName) if wishRecord.Error != nil { return wishRecord.Error } else if wishRecord.RowsAffected != 1 { return errors.New("Name not changed in database.") } - wishRecord = Instance.Model(wish).Where("`wishes`.enabled = ?", 1).Where("`wishes`.ID = ?", wishID).Update("note", wishNote) + wishRecord = Instance.Model(wish).Where(&models.Wish{Enabled: true}).Where(&models.GormModel{ID: wishID}).Update("note", wishNote) if wishRecord.Error != nil { return wishRecord.Error } else if wishRecord.RowsAffected != 1 { return errors.New("Note not changed in database.") } - wishRecord = Instance.Model(wish).Where("`wishes`.enabled = ?", 1).Where("`wishes`.ID = ?", wishID).Update("url", wishURL) + wishRecord = Instance.Model(wish).Where(&models.Wish{Enabled: true}).Where(&models.GormModel{ID: wishID}).Update("url", wishURL) if wishRecord.Error != nil { return wishRecord.Error } else if wishRecord.RowsAffected != 1 { return errors.New("URL not changed in database.") } - wishRecord = Instance.Model(wish).Where("`wishes`.enabled = ?", 1).Where("`wishes`.ID = ?", wishID).Update("price", wishprice) + wishRecord = Instance.Model(wish).Where(&models.Wish{Enabled: true}).Where(&models.GormModel{ID: wishID}).Update("price", wishPrice) if wishRecord.Error != nil { return wishRecord.Error } else if wishRecord.RowsAffected != 1 { @@ -78,7 +78,11 @@ func GetWishesFromWishlist(WishlistID uuid.UUID) (bool, []models.Wish, error) { var wishes []models.Wish - wishrecords := Instance.Order("created_at ASC").Where("`wishes`.enabled = ?", 1).Where("`wishes`.wishlist_id = ?", WishlistID).Joins("JOIN `users` on `users`.id = `wishes`.owner_id").Where("`users`.enabled = ?", 1).Find(&wishes) + wishrecords := Instance. + Order("created_at ASC"). + Where(&models.Wish{Enabled: true, WishlistID: WishlistID}). + Joins("JOIN users ON users.id = wishes.owner_id", Instance.Where(&models.User{Enabled: true})). + Find(&wishes) if wishrecords.Error != nil { return false, []models.Wish{}, wishrecords.Error } else if wishrecords.RowsAffected < 1 { @@ -95,10 +99,9 @@ func GetWishClaimFromWish(WishID uuid.UUID) ([]models.WishClaimObject, error) { var wisharray_with_user []models.WishClaimObject wishclaimrecords := Instance. - Where("`wish_claims`.enabled = ?", 1). - Where("`wish_claims`.wish_id = ?", WishID). - Joins("JOIN `users` on `users`.id = `wish_claims`.user_id"). - Where("`users`.enabled = ?", 1).Find(&wish_claim) + Where(&models.WishClaim{Enabled: true, WishID: WishID}). + Joins("JOIN users ON users.id = wish_claims.user_id", Instance.Where(&models.User{Enabled: true})). + Find(&wish_claim) if wishclaimrecords.Error != nil { return []models.WishClaimObject{}, wishclaimrecords.Error @@ -127,7 +130,7 @@ func GetWishClaimFromWish(WishID uuid.UUID) ([]models.WishClaimObject, error) { // Verify if a user ID is an owner of a wish func VerifyUserOwnershipToWish(UserID uuid.UUID, WishID uuid.UUID) (bool, error) { var wish models.Wish - wishrecord := Instance.Where("`wishes`.enabled = ?", 1).Where("`wishes`.id = ?", WishID).Where("`wishes`.owner_id = ?", UserID).Find(&wish) + wishrecord := Instance.Where(&models.Wish{Enabled: true, OwnerID: UserID}).Where(&models.GormModel{ID: WishID}).Find(&wish) if wishrecord.Error != nil { return false, wishrecord.Error } else if wishrecord.RowsAffected != 1 { @@ -139,7 +142,7 @@ func VerifyUserOwnershipToWish(UserID uuid.UUID, WishID uuid.UUID) (bool, error) // Verify if a user ID is an owner of a wish func VerifyUserOwnershipToWishClaimByWish(UserID uuid.UUID, WishID uuid.UUID) (bool, error) { var wishclaim models.WishClaim - wishclaimrecord := Instance.Where("`wish_claims`.enabled = ?", 1).Where("`wish_claims`.wish_id = ?", WishID).Where("`wish_claims`.user_id = ?", UserID).Find(&wishclaim) + wishclaimrecord := Instance.Where(&models.WishClaim{Enabled: true, WishID: WishID, UserID: UserID}).Find(&wishclaim) if wishclaimrecord.Error != nil { return false, wishclaimrecord.Error } else if wishclaimrecord.RowsAffected != 1 { @@ -151,7 +154,10 @@ func VerifyUserOwnershipToWishClaimByWish(UserID uuid.UUID, WishID uuid.UUID) (b // Verify if a user ID is an owner of a wish func VerifyWishIsClaimed(WishID uuid.UUID) (bool, error) { var wishclaim models.WishClaim - wishclaimrecord := Instance.Where("`wish_claims`.enabled = ?", 1).Where("`wish_claims`.wish_id = ?", WishID).Joins("JOIN `users` on `users`.id = `wish_claims`.user_id").Where("`users`.enabled = ?", 1).Find(&wishclaim) + wishclaimrecord := Instance. + Where(&models.WishClaim{Enabled: true, WishID: WishID}). + Joins("JOIN users ON users.id = wish_claims.user_id", Instance.Where(&models.User{Enabled: true})). + Find(&wishclaim) if wishclaimrecord.Error != nil { return false, wishclaimrecord.Error } else if wishclaimrecord.RowsAffected != 1 { @@ -163,7 +169,7 @@ func VerifyWishIsClaimed(WishID uuid.UUID) (bool, error) { // Set wish claim to disabled func DeleteWishClaimByUserAndWish(WishID uuid.UUID, UserID uuid.UUID) error { var wishclaim models.WishClaim - wishclaimrecords := Instance.Model(wishclaim).Where("`wish_claims`.wish_id = ?", WishID).Where("`wish_claims`.user_id = ?", UserID).Update("enabled", 0) + wishclaimrecords := Instance.Model(wishclaim).Where(&models.WishClaim{WishID: WishID, UserID: UserID}).Update("enabled", 0) if wishclaimrecords.Error != nil { return wishclaimrecords.Error } @@ -176,7 +182,7 @@ func DeleteWishClaimByUserAndWish(WishID uuid.UUID, UserID uuid.UUID) error { // Set wish to disabled func DeleteWish(WishID uuid.UUID) error { var wish models.Wish - wishrecords := Instance.Model(wish).Where("`wishes`.id = ?", WishID).Update("enabled", 0) + wishrecords := Instance.Model(wish).Where(&models.GormModel{ID: WishID}).Update("enabled", false) if wishrecords.Error != nil { return wishrecords.Error } @@ -191,10 +197,8 @@ func GetWishlistByWishID(wishID uuid.UUID) (bool, models.Wishlist, error) { var wishlist models.Wishlist wishlistRecords := Instance. - Where("`wishlists`.enabled = ?", 1). - Joins("JOIN `wishes` on `wishlists`.id = `wishes`.wishlist_id"). - Where("`wishes`.enabled = ?", 1). - Where("`wishes`.id = ?", wishID). + Where(&models.Wishlist{Enabled: true}). + Joins("JOIN wishes ON wishlists.id = wishes.wishlist_id", Instance.Where(&models.Wish{Enabled: true}).Where(&models.GormModel{ID: wishID})). Find(&wishlist) if wishlistRecords.Error != nil { diff --git a/database/wishlist.go b/database/wishlist.go index 58c02f1..69f0ef5 100644 --- a/database/wishlist.go +++ b/database/wishlist.go @@ -2,6 +2,7 @@ package database import ( "aunefyren/poenskelisten/models" + "aunefyren/poenskelisten/utilities" "errors" "time" @@ -13,7 +14,7 @@ func UpdateWishlistValuesByID(wishlistID uuid.UUID, wishlistName string, wishlis var wishlist models.Wishlist - wishlistRecord := Instance.Model(wishlist).Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.ID = ?", wishlistID).Update("name", wishlistName) + wishlistRecord := Instance.Model(wishlist).Where(&models.Wishlist{GormModel: models.GormModel{ID: wishlistID}, Enabled: true}).Update("name", wishlistName) if wishlistRecord.Error != nil { return wishlistRecord.Error } @@ -21,7 +22,7 @@ func UpdateWishlistValuesByID(wishlistID uuid.UUID, wishlistName string, wishlis return errors.New("Name not changed in database.") } - wishlistRecord = Instance.Model(wishlist).Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.ID = ?", wishlistID).Update("description", wishlistDesc) + wishlistRecord = Instance.Model(wishlist).Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: wishlistID}).Update("description", wishlistDesc) if wishlistRecord.Error != nil { return wishlistRecord.Error } @@ -29,7 +30,7 @@ func UpdateWishlistValuesByID(wishlistID uuid.UUID, wishlistName string, wishlis return errors.New("Description not changed in database.") } - wishlistRecord = Instance.Model(wishlist).Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.ID = ?", wishlistID).Update("date", wishlistExpiration) + wishlistRecord = Instance.Model(wishlist).Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: wishlistID}).Update("date", wishlistExpiration) if wishlistRecord.Error != nil { return wishlistRecord.Error } @@ -37,7 +38,7 @@ func UpdateWishlistValuesByID(wishlistID uuid.UUID, wishlistName string, wishlis return errors.New("Expiration not changed in database.") } - wishlistRecord = Instance.Model(wishlist).Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.ID = ?", wishlistID).Update("claimable", wishlistClaimable) + wishlistRecord = Instance.Model(wishlist).Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: wishlistID}).Update("claimable", wishlistClaimable) if wishlistRecord.Error != nil { return wishlistRecord.Error } @@ -45,7 +46,7 @@ func UpdateWishlistValuesByID(wishlistID uuid.UUID, wishlistName string, wishlis return errors.New("Claimability not changed in database.") } - wishlistRecord = Instance.Model(wishlist).Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.ID = ?", wishlistID).Update("expires", wishlistExpires) + wishlistRecord = Instance.Model(wishlist).Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: wishlistID}).Update("expires", wishlistExpires) if wishlistRecord.Error != nil { return wishlistRecord.Error } @@ -53,7 +54,7 @@ func UpdateWishlistValuesByID(wishlistID uuid.UUID, wishlistName string, wishlis return errors.New("Expiration not changed in database.") } - wishlistRecord = Instance.Model(wishlist).Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.ID = ?", wishlistID).Update("public", wishlistPublic) + wishlistRecord = Instance.Model(wishlist).Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: wishlistID}).Update("public", wishlistPublic) if wishlistRecord.Error != nil { return wishlistRecord.Error } @@ -61,7 +62,7 @@ func UpdateWishlistValuesByID(wishlistID uuid.UUID, wishlistName string, wishlis return errors.New("Public state not changed in database.") } - wishlistRecord = Instance.Model(wishlist).Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.ID = ?", wishlistID).Update("public_hash", wishlistPublicHash) + wishlistRecord = Instance.Model(wishlist).Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: wishlistID}).Update("public_hash", wishlistPublicHash) if wishlistRecord.Error != nil { return wishlistRecord.Error } @@ -75,8 +76,6 @@ func UpdateWishlistValuesByID(wishlistID uuid.UUID, wishlistName string, wishlis // Create wishlist in DB func CreateWishlistInDB(wishlistdb models.Wishlist) (wishlist models.Wishlist, err error) { - wishlist = models.Wishlist{} - err = nil record := Instance.Create(&wishlistdb) if record.Error != nil { @@ -95,7 +94,7 @@ func GetWishlistByWishlistID(wishlistID uuid.UUID) (bool, models.Wishlist, error var wishlist models.Wishlist - wishlistRecord := Instance.Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.id = ?", wishlistID).Find(&wishlist) + wishlistRecord := Instance.Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: wishlistID}).Find(&wishlist) if wishlistRecord.Error != nil { return false, models.Wishlist{}, wishlistRecord.Error @@ -110,9 +109,12 @@ func GetWishlistByWishlistID(wishlistID uuid.UUID) (bool, models.Wishlist, error // Get wishlist collabs who are members of wishlist func GetWishlistCollaboratorsFromWishlist(WishlistID uuid.UUID) (wishlistColab []models.WishlistCollaborator, err error) { wishlistColab = []models.WishlistCollaborator{} - err = nil - userRecords := Instance.Where("`wishlist_collaborators`.enabled = ?", 1).Joins("JOIN `users` on `wishlist_collaborators`.user_id = `users`.id").Where("`users`.enabled = ?", 1).Joins("JOIN `wishlists` on `wishlist_collaborators`.wishlist_id = `wishlists`.id").Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.id = ?", WishlistID).Find(&wishlistColab) + userRecords := Instance. + Where(&models.WishlistCollaborator{Enabled: true}). + Joins("JOIN users ON wishlist_collaborators.user_id = users.id", Instance.Where(&models.User{Enabled: true})). + Joins("JOIN wishlists ON wishlist_collaborators.wishlist_id = wishlists.id", Instance.Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: WishlistID})). + Find(&wishlistColab) if userRecords.Error != nil { return wishlistColab, userRecords.Error } @@ -123,9 +125,12 @@ func GetWishlistCollaboratorsFromWishlist(WishlistID uuid.UUID) (wishlistColab [ // Get wishlist collab by id func GetWishlistCollaboratorByUserIDAndWishlistID(WishlistID uuid.UUID, UserID uuid.UUID) (wishlistColab models.WishlistCollaborator, err error) { wishlistColab = models.WishlistCollaborator{} - err = nil - userRecords := Instance.Where("`wishlist_collaborators`.enabled = ?", 1).Joins("JOIN `users` on `wishlist_collaborators`.user_id = `users`.id").Where("`users`.enabled = ?", 1).Joins("JOIN `wishlists` on `wishlist_collaborators`.wishlist_id = `wishlists`.id").Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.id = ?", WishlistID).Where("`users`.id = ?", UserID).Find(&wishlistColab) + userRecords := Instance. + Where(&models.WishlistCollaborator{Enabled: true}). + Joins("JOIN users ON wishlist_collaborators.user_id = users.id", Instance.Where(&models.User{Enabled: true}).Where(&models.GormModel{ID: UserID})). + Joins("JOIN wishlists ON wishlist_collaborators.wishlist_id = wishlists.id", Instance.Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: WishlistID})). + Find(&wishlistColab) if userRecords.Error != nil { return wishlistColab, userRecords.Error } @@ -154,7 +159,10 @@ func VerifyWishlistCollaboratorToWishlist(WishlistID uuid.UUID, UserID uuid.UUID err = nil wishlistColab := models.WishlistCollaborator{} - wishlistmembershipprecord := Instance.Where("`wishlist_collaborators`.enabled = ?", 1).Where("`wishlist_collaborators`.wishlist_id = ?", WishlistID).Where("`wishlist_collaborators`.user_id = ?", UserID).Joins("JOIN `users` on `wishlist_collaborators`.user_id = `users`.id").Where("`users`.enabled = ?", 1).Find(&wishlistColab) + wishlistmembershipprecord := Instance. + Where(&models.WishlistCollaborator{Enabled: true, WishlistID: WishlistID, UserID: UserID}). + Joins("JOIN users ON wishlist_collaborators.user_id = users.id", Instance.Where(&models.User{Enabled: true})). + Find(&wishlistColab) if wishlistmembershipprecord.Error != nil { return verified, wishlistmembershipprecord.Error } else if wishlistmembershipprecord.RowsAffected != 1 { @@ -167,9 +175,8 @@ func VerifyWishlistCollaboratorToWishlist(WishlistID uuid.UUID, UserID uuid.UUID // Set wishlist membership to disabled func DeleteWishlistCollaboratorByWishlistCollaboratorID(WishlistCollaboratorID uuid.UUID) (err error) { wishlistCollaborator := models.WishlistCollaborator{} - err = nil - wishlistmembershiprecords := Instance.Model(wishlistCollaborator).Where("`wishlist_collaborators`.ID= ?", WishlistCollaboratorID).Update("enabled", 0) + wishlistmembershiprecords := Instance.Model(wishlistCollaborator).Where(&models.GormModel{ID: WishlistCollaboratorID}).Update("enabled", 0) if wishlistmembershiprecords.Error != nil { return wishlistmembershiprecords.Error } @@ -185,7 +192,12 @@ func GetWishlistsByUserIDThroughWishlistCollaborations(UserID uuid.UUID) (wishli wishlists = []models.Wishlist{} err = nil - wishlistRecords := Instance.Order("`wishlists`.date desc, `wishlists`.name").Where("`wishlists`.enabled = ?", 1).Joins("JOIN `wishlist_collaborators` on `wishlists`.id = `wishlist_collaborators`.wishlist_id").Where("`wishlist_collaborators`.enabled = ?", 1).Where("`wishlist_collaborators`.user_id = ?", UserID).Joins("JOIN `users` on `wishlist_collaborators`.user_id = `users`.id").Where("`users`.enabled = ?", 1).Find(&wishlists) + // Order("wishlists.date desc, wishlists.name"). + wishlistRecords := Instance. + Where(&models.Wishlist{Enabled: true}). + Joins("JOIN wishlist_collaborators ON wishlists.id = wishlist_collaborators.wishlist_id", Instance.Where(&models.WishlistCollaborator{Enabled: true, UserID: UserID})). + Joins("JOIN users ON wishlist_collaborators.user_id = users.id", Instance.Where(&models.User{Enabled: true})). + Find(&wishlists) if wishlistRecords.Error != nil { return []models.Wishlist{}, wishlistRecords.Error @@ -198,14 +210,11 @@ func GetWishlistsByUserIDThroughWishlistCollaborations(UserID uuid.UUID) (wishli func GetWishlistsFromGroup(GroupID uuid.UUID) ([]models.Wishlist, error) { var wishlists []models.Wishlist wishlistrecords := Instance. - Where("`wishlists`.enabled = ?", 1). - Joins("JOIN wishlist_memberships on wishlist_memberships.wishlist_id = wishlists.id"). - Where("`wishlist_memberships`.group_id = ?", GroupID). - Where("`wishlist_memberships`.enabled = ?", 1). - Joins("JOIN `groups` on `wishlist_memberships`.group_id = `groups`.ID"). - Where("`groups`.enabled = ?", 1). - Joins("JOIN `users` on `wishlists`.owner_id = `users`.id"). - Where("`users`.enabled = ?", 1).Find(&wishlists) + Where(&models.Wishlist{Enabled: true}). + Joins("JOIN wishlist_memberships ON wishlist_memberships.wishlist_id = wishlists.id", Instance.Where(&models.WishlistMembership{Enabled: true, GroupID: GroupID})). + Joins("JOIN groups ON wishlist_memberships.group_id = groups.ID", Instance.Where(&models.Group{Enabled: true})). + Joins("JOIN users ON wishlists.owner_id = users.id", Instance.Where(&models.User{Enabled: true})). + Find(&wishlists) if wishlistrecords.Error != nil { return []models.Wishlist{}, wishlistrecords.Error @@ -221,7 +230,11 @@ func GetOwnedWishlists(UserID uuid.UUID) (wishlists []models.Wishlist, err error wishlists = []models.Wishlist{} err = nil - wishlistrecords := Instance.Order("`wishlists`.date desc, `wishlists`.name").Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.owner_id = ?", UserID).Joins("JOIN users on `users`.id = `wishlists`.owner_id").Where("`users`.enabled = ?", 1).Find(&wishlists) + wishlistrecords := Instance. + Where(&models.Wishlist{Enabled: true, OwnerID: UserID}). + Joins("JOIN users ON users.id = wishlists.owner_id", Instance.Where(&models.User{Enabled: true})). + Order("wishlists.date desc, wishlists.name"). + Find(&wishlists) if wishlistrecords.Error != nil { return []models.Wishlist{}, wishlistrecords.Error @@ -233,7 +246,7 @@ func GetOwnedWishlists(UserID uuid.UUID) (wishlists []models.Wishlist, err error // Get all wishlists a user is an owner of func GetWishlist(WishlistID uuid.UUID) (models.Wishlist, error) { var wishlist models.Wishlist - wishlistrecords := Instance.Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.id = ?", WishlistID).Find(&wishlist) + wishlistrecords := Instance.Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: WishlistID}).Find(&wishlist) if wishlistrecords.Error != nil { return models.Wishlist{}, wishlistrecords.Error @@ -247,7 +260,7 @@ func GetWishlist(WishlistID uuid.UUID) (models.Wishlist, error) { // Verify if a wish name in wishlist is unique func VerifyUniqueWishNameinWishlist(WishName string, WishlistID uuid.UUID) (bool, error) { var wish models.Wish - wishesrecord := Instance.Where("`wishes`.enabled = ?", 1).Where("`wishes`.wishlist_id = ?", WishlistID).Where("`wishes`.name = ?", WishName).Find(&wish) + wishesrecord := Instance.Where(&models.Wish{Enabled: true, WishlistID: WishlistID, Name: WishName}).Find(&wish) if wishesrecord.Error != nil { return false, wishesrecord.Error } else if wishesrecord.RowsAffected != 0 { @@ -259,7 +272,7 @@ func VerifyUniqueWishNameinWishlist(WishName string, WishlistID uuid.UUID) (bool // Verify if a wishlist name in group is unique func VerifyUniqueWishlistNameForUser(WishlistName string, UserID uuid.UUID) (bool, error) { var wishlist models.Wishlist - wishlistrecord := Instance.Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.owner_id = ?", UserID).Where("`wishlists`.name = ?", WishlistName).Find(&wishlist) + wishlistrecord := Instance.Where(&models.Wishlist{Enabled: true, OwnerID: UserID, Name: WishlistName}).Find(&wishlist) if wishlistrecord.Error != nil { return false, wishlistrecord.Error } else if wishlistrecord.RowsAffected != 0 { @@ -271,7 +284,7 @@ func VerifyUniqueWishlistNameForUser(WishlistName string, UserID uuid.UUID) (boo // Get owner id of wishlist func GetWishlistOwner(WishlistID uuid.UUID) (uuid.UUID, error) { var wishlist models.Wishlist - wishlistrecord := Instance.Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.id = ?", WishlistID).Find(&wishlist) + wishlistrecord := Instance.Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: WishlistID}).Find(&wishlist) if wishlistrecord.Error != nil { return uuid.UUID{}, wishlistrecord.Error } else if wishlistrecord.RowsAffected != 1 { @@ -284,7 +297,11 @@ func GetWishlistOwner(WishlistID uuid.UUID) (uuid.UUID, error) { // Verify if a group ID is a member of a wishlist func VerifyUserMembershipToGroupmembershipToWishlist(UserID uuid.UUID, WishlistID uuid.UUID) (bool, error) { var wishlistmembership models.WishlistMembership - wishlistmembershiprecord := Instance.Where("`wishlist_memberships`.enabled = ?", 1).Where("`wishlist_memberships`.wishlist_id = ?", WishlistID).Joins("JOIN `groups` on `groups`.id = `wishlist_memberships`.group_id").Where("`groups`.enabled = ?", 1).Joins("JOIN `group_memberships` on `group_memberships`.group_id = `groups`.id").Where("`group_memberships`.enabled = ?", 1).Where("`group_memberships`.member_id = ?", UserID).Find(&wishlistmembership) + wishlistmembershiprecord := Instance. + Where(&models.WishlistMembership{WishlistID: WishlistID, Enabled: true}). + Joins("JOIN groups ON groups.id = wishlist_memberships.group_id", Instance.Where(&models.Group{Enabled: true})). + Joins("JOIN group_memberships ON group_memberships.group_id = groups.id", Instance.Where(&models.GroupMembership{Enabled: true, MemberID: UserID})). + Find(&wishlistmembership) if wishlistmembershiprecord.Error != nil { return false, wishlistmembershiprecord.Error } else if wishlistmembershiprecord.RowsAffected != 1 { @@ -296,7 +313,7 @@ func VerifyUserMembershipToGroupmembershipToWishlist(UserID uuid.UUID, WishlistI // Verify if a user ID is an owner of a wishlist func VerifyUserOwnershipToWishlist(UserID uuid.UUID, WishlistID uuid.UUID) (bool, error) { var wishlist models.Wishlist - wishlistrecord := Instance.Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.id = ?", WishlistID).Where("`wishlists`.owner_id = ?", UserID).Find(&wishlist) + wishlistrecord := Instance.Where(&models.Wishlist{Enabled: true, OwnerID: UserID}).Where(&models.GormModel{ID: WishlistID}).Find(&wishlist) if wishlistrecord.Error != nil { return false, wishlistrecord.Error } else if wishlistrecord.RowsAffected != 1 { @@ -310,7 +327,14 @@ func GetUserMembersFromWishlist(WishlistID uuid.UUID) ([]models.User, error) { var users []models.User var group_memberships []models.GroupMembership - membershiprecords := Instance.Where("`group_memberships`.enabled = ?", 1).Joins("JOIN `groups` on `group_memberships`.group_id = `groups`.id").Where("`groups`.enabled = ?", 1).Joins("JOIN `wishlist_memberships` on `wishlist_memberships`.group_id = `groups`.id").Where("`wishlist_memberships`.enabled = ?", 1).Joins("JOIN `wishlists` on `wishlists`.id = `wishlist_memberships`.wishlist_id").Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.id = ?", WishlistID).Joins("JOIN `users` on `group_memberships`.group_id = `users`.id").Where("`users`.enabled = ?", 1).Where("`group_memberships`.group_id != `wishlists`.owner_id").Find(&group_memberships) + membershiprecords := Instance. + Where(&models.GroupMembership{Enabled: true}). + Joins("JOIN groups ON group_memberships.group_id = groups.id", Instance.Where(&models.Group{Enabled: true})). + Joins("JOIN wishlist_memberships ON wishlist_memberships.group_id = groups.id", Instance.Where(&models.GroupMembership{Enabled: true})). + Joins("JOIN wishlists ON wishlists.id = wishlist_memberships.wishlist_id", Instance.Where(&models.Wishlist{Enabled: true}).Where(&models.GormModel{ID: WishlistID})). + Joins("JOIN users ON group_memberships.group_id = users.id", Instance.Where(&models.User{Enabled: true})). + Where("group_memberships.group_id != wishlists.owner_id"). + Find(&group_memberships) if membershiprecords.Error != nil { return []models.User{}, membershiprecords.Error } @@ -335,7 +359,7 @@ func GetMembershipIDForGroupToWishlist(WishlistID uuid.UUID, GroupID uuid.UUID) membershipFound = false err = nil - wishlistmembershiprecord := Instance.Where("`wishlist_memberships`.enabled = ?", 1).Where("`wishlist_memberships`.wishlist_id = ?", WishlistID).Where("`wishlist_memberships`.group_id = ?", GroupID).Find(&wishlistMembership) + wishlistmembershiprecord := Instance.Where(&models.WishlistMembership{WishlistID: WishlistID, Enabled: true, GroupID: GroupID}).Find(&wishlistMembership) if wishlistmembershiprecord.Error != nil { return membershipFound, wishlistMembership, wishlistmembershiprecord.Error } else if wishlistmembershiprecord.RowsAffected != 1 { @@ -349,7 +373,7 @@ func GetMembershipIDForGroupToWishlist(WishlistID uuid.UUID, GroupID uuid.UUID) func GetPublicWishListByWishlistHash(wishlistHash uuid.UUID) (bool, models.Wishlist, error) { var wishlist models.Wishlist - wishlistRecord := Instance.Where("`wishlists`.enabled = ?", 1).Where("`wishlists`.public = ?", 1).Where("`wishlists`.public_hash = ?", wishlistHash).Find(&wishlist) + wishlistRecord := Instance.Where(&models.Wishlist{Enabled: true, PublicHash: wishlistHash, Public: &utilities.DBTrue}).Find(&wishlist) if wishlistRecord.Error != nil { return false, models.Wishlist{}, wishlistRecord.Error diff --git a/go.mod b/go.mod index 21732c1..35a5195 100644 --- a/go.mod +++ b/go.mod @@ -12,24 +12,24 @@ require ( github.com/google/uuid v1.6.0 github.com/nfnt/resize v0.0.0-20180221191011-83c6a9932646 github.com/thanhpk/randstr v1.0.6 - golang.org/x/crypto v0.28.0 + golang.org/x/crypto v0.29.0 gorm.io/driver/mysql v1.5.7 - gorm.io/driver/postgres v1.5.9 + gorm.io/driver/postgres v1.5.10 gorm.io/driver/sqlite v1.5.6 gorm.io/gorm v1.25.12 ) require ( filippo.io/edwards25519 v1.1.0 // indirect - github.com/bytedance/sonic v1.12.3 // indirect + github.com/bytedance/sonic v1.12.5 // indirect github.com/bytedance/sonic/loader v0.2.1 // indirect github.com/cloudwego/base64x v0.1.4 // indirect github.com/cloudwego/iasm v0.2.0 // indirect - github.com/gabriel-vasile/mimetype v1.4.6 // indirect + github.com/gabriel-vasile/mimetype v1.4.7 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect github.com/go-playground/universal-translator v0.18.1 // indirect - github.com/go-playground/validator/v10 v10.22.1 // indirect + github.com/go-playground/validator/v10 v10.23.0 // indirect github.com/go-sql-driver/mysql v1.8.1 // indirect github.com/goccy/go-json v0.10.3 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect @@ -39,7 +39,7 @@ require ( github.com/jinzhu/inflection v1.0.0 // indirect github.com/jinzhu/now v1.1.5 // indirect github.com/json-iterator/go v1.1.12 // indirect - github.com/klauspost/cpuid/v2 v2.2.8 // indirect + github.com/klauspost/cpuid/v2 v2.2.9 // indirect github.com/kr/text v0.2.0 // indirect github.com/leodido/go-urn v1.4.0 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -49,12 +49,12 @@ require ( github.com/pelletier/go-toml/v2 v2.2.3 // indirect github.com/twitchyliquid64/golang-asm v0.15.1 // indirect github.com/ugorji/go/codec v1.2.12 // indirect - golang.org/x/arch v0.11.0 // indirect - golang.org/x/net v0.30.0 // indirect - golang.org/x/sync v0.8.0 // indirect - golang.org/x/sys v0.26.0 // indirect - golang.org/x/text v0.19.0 // indirect - google.golang.org/protobuf v1.35.1 // indirect + golang.org/x/arch v0.12.0 // indirect + golang.org/x/net v0.31.0 // indirect + golang.org/x/sync v0.9.0 // indirect + golang.org/x/sys v0.27.0 // indirect + golang.org/x/text v0.20.0 // indirect + google.golang.org/protobuf v1.35.2 // indirect gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect gopkg.in/mail.v2 v2.3.1 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index a356637..c55d7d1 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA= filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4= -github.com/bytedance/sonic v1.12.3 h1:W2MGa7RCU1QTeYRTPE3+88mVC0yXmsRQRChiyVocVjU= -github.com/bytedance/sonic v1.12.3/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= +github.com/bytedance/sonic v1.12.5 h1:hoZxY8uW+mT+OpkcUWw4k0fDINtOcVavEsGfzwzFU/w= +github.com/bytedance/sonic v1.12.5/go.mod h1:B8Gt/XvtZ3Fqj+iSKMypzymZxw/FVwgIGKzMzT9r/rk= github.com/bytedance/sonic/loader v0.1.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= github.com/bytedance/sonic/loader v0.2.1 h1:1GgorWTqf12TA8mma4DDSbaQigE2wOgQo7iCjjJv3+E= github.com/bytedance/sonic/loader v0.2.1/go.mod h1:ncP89zfokxS5LZrJxl5z0UJcsk4M4yY2JpfqGeCtNLU= @@ -13,8 +13,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3 github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= -github.com/gabriel-vasile/mimetype v1.4.6 h1:3+PzJTKLkvgjeTbts6msPJt4DixhT4YtFNf1gtGe3zc= -github.com/gabriel-vasile/mimetype v1.4.6/go.mod h1:JX1qVKqZd40hUPpAfiNTe0Sne7hdfKSbOqqmkq8GCXc= +github.com/gabriel-vasile/mimetype v1.4.7 h1:SKFKl7kD0RiPdbht0s7hFtjl489WcQ1VyPW8ZzUMYCA= +github.com/gabriel-vasile/mimetype v1.4.7/go.mod h1:GDlAgAyIRT27BhFl53XNAFtfjzOkLaF35JdEG0P7LtU= github.com/gin-contrib/cors v1.7.2 h1:oLDHxdg8W/XDoN/8zamqk/Drgt4oVZDvaV0YmvVICQw= github.com/gin-contrib/cors v1.7.2/go.mod h1:SUJVARKgQ40dmrzgXEVxj2m7Ig1v1qIboQkPDTQ9t2E= github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE= @@ -29,8 +29,8 @@ github.com/go-playground/locales v0.14.1 h1:EWaQ/wswjilfKLTECiXz7Rh+3BjFhfDFKv/o github.com/go-playground/locales v0.14.1/go.mod h1:hxrqLVvrK65+Rwrd5Fc6F2O76J/NuW9t0sjnWqG1slY= github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJnYK9S473LQFuzCbDbfSFY= github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY= -github.com/go-playground/validator/v10 v10.22.1 h1:40JcKH+bBNGFczGuoBYgX4I6m/i27HYW8P9FDk5PbgA= -github.com/go-playground/validator/v10 v10.22.1/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= +github.com/go-playground/validator/v10 v10.23.0 h1:/PwmTwZhS0dPkav3cdK9kV1FsAmrL8sThn8IHr/sO+o= +github.com/go-playground/validator/v10 v10.23.0/go.mod h1:dbuPbCMFw/DrkbEynArYaCwl3amGuJotoKCe95atGMM= github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI= github.com/go-sql-driver/mysql v1.8.1 h1:LedoTUt/eveggdHS9qUFC1EFSa8bU2+1pZjSRpvNJ1Y= github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqwuVSjsCm7DZg= @@ -58,8 +58,8 @@ github.com/jinzhu/now v1.1.5/go.mod h1:d3SSVoowX0Lcu0IBviAWJpolVfI5UJVZZ7cO71lE/ github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg= -github.com/klauspost/cpuid/v2 v2.2.8 h1:+StwCXwm9PdpiEkPyzBXIy+M9KUb4ODm0Zarf1kS5BM= -github.com/klauspost/cpuid/v2 v2.2.8/go.mod h1:Lcz8mBdAVJIBVzewtcLocK12l3Y+JytZYpaMropDUws= +github.com/klauspost/cpuid/v2 v2.2.9 h1:66ze0taIn2H33fBvCkXuv9BmCwDfafmiIVpKV9kKGuY= +github.com/klauspost/cpuid/v2 v2.2.9/go.mod h1:rqkxqrZ1EhYM9G+hXH7YdowN5R5RGN6NK4QwQ3WMXF8= github.com/knz/go-libedit v1.10.1/go.mod h1:MZTVkCWyz0oBc7JOWP3wNAzd002ZbM/5hgShxwh4x8M= github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0= github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk= @@ -100,24 +100,23 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08= github.com/ugorji/go/codec v1.2.12 h1:9LC83zGrHhuUA9l16C9AHXAqEV/2wBQ4nkvumAE65EE= github.com/ugorji/go/codec v1.2.12/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg= -golang.org/x/arch v0.11.0 h1:KXV8WWKCXm6tRpLirl2szsO5j/oOODwZf4hATmGVNs4= -golang.org/x/arch v0.11.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= -golang.org/x/crypto v0.28.0 h1:GBDwsMXVQi34v5CCYUm2jkJvu4cbtru2U4TN2PSyQnw= -golang.org/x/crypto v0.28.0/go.mod h1:rmgy+3RHxRZMyY0jjAJShp2zgEdOqj2AO7U0pYmeQ7U= -golang.org/x/net v0.30.0 h1:AcW1SDZMkb8IpzCdQUaIq2sP4sZ4zw+55h6ynffypl4= -golang.org/x/net v0.30.0/go.mod h1:2wGyMJ5iFasEhkwi13ChkO/t1ECNC4X4eBKkVFyYFlU= -golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= -golang.org/x/sync v0.8.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= -golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/arch v0.12.0 h1:UsYJhbzPYGsT0HbEdmYcqtCv8UNGvnaL561NnIUvaKg= +golang.org/x/arch v0.12.0/go.mod h1:FEVrYAQjsQXMVJ1nsMoVVXPZg6p2JE2mx8psSWTDQys= +golang.org/x/crypto v0.29.0 h1:L5SG1JTTXupVV3n6sUqMTeWbjAyfPwoda2DLX8J8FrQ= +golang.org/x/crypto v0.29.0/go.mod h1:+F4F4N5hv6v38hfeYwTdx20oUvLLc+QfrE9Ax9HtgRg= +golang.org/x/net v0.31.0 h1:68CPQngjLL0r2AlUKiSxtQFKvzRVbnzLwMUn5SzcLHo= +golang.org/x/net v0.31.0/go.mod h1:P4fl1q7dY2hnZFxEk4pPSkDHF+QqjitcnDjUQyMM+pM= +golang.org/x/sync v0.9.0 h1:fEo0HyrW1GIgZdpbhCRO0PkJajUS5H9IFUztCgEo2jQ= +golang.org/x/sync v0.9.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.26.0 h1:KHjCJyddX0LoSTb3J+vWpupP9p0oznkqVk/IfjymZbo= -golang.org/x/sys v0.26.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= -golang.org/x/text v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= -golang.org/x/text v0.19.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY= +golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s= +golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug= +golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= -google.golang.org/protobuf v1.35.1 h1:m3LfL6/Ca+fqnjnlqQXNpFPABW1UD7mjh8KO2mKFytA= -google.golang.org/protobuf v1.35.1/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= +google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io= +google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc h1:2gGKlE2+asNV9m7xrywl36YYNnBG5ZQ0r/BOOxqPpmk= gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc/go.mod h1:m7x9LTH6d71AHyAX77c9yqWCCa3UKHcVEj9y7hAtKDk= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= @@ -130,8 +129,8 @@ gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo= gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM= -gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8= -gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= +gorm.io/driver/postgres v1.5.10 h1:7Lggqempgy496c0WfHXsYWxk3Th+ZcW66/21QhVFdeE= +gorm.io/driver/postgres v1.5.10/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI= gorm.io/driver/sqlite v1.5.6 h1:fO/X46qn5NUEEOZtnjJRWRzZMe8nqJiQ9E+0hi+hKQE= gorm.io/driver/sqlite v1.5.6/go.mod h1:U+J8craQU6Fzkcvu8oLeAQmi50TkwPEhHDEjQZXDah4= gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8= diff --git a/main.go b/main.go index fd62e18..ed78284 100644 --- a/main.go +++ b/main.go @@ -118,7 +118,10 @@ func main() { log.Println("Creating new private key.") Config.PrivateKey = randstr.Hex(32) - config.SaveConfig(Config) + err = config.SaveConfig(Config) + if err != nil { + panic(err) + } } err = auth.SetPrivateKey(Config.PrivateKey) diff --git a/middlewares/auth.go b/middlewares/auth.go index e38fdb1..15796ed 100644 --- a/middlewares/auth.go +++ b/middlewares/auth.go @@ -57,6 +57,9 @@ func AuthFunction(context *gin.Context, admin bool) (success bool, errorString s // Check if the user is verified verified, err := database.VerifyUserIsVerified(userID) + if err != nil { + panic(err) + } if !verified { // Verify user has verification code diff --git a/utilities/migrate.go b/utilities/migrate.go index bb26410..35310f9 100644 --- a/utilities/migrate.go +++ b/utilities/migrate.go @@ -57,7 +57,7 @@ func MigrateSQL(sqlContent *bufio.Scanner) (modifiedSQL2 string, err error) { currentMode = "none" currentTable = "none" } else { - // log.Println("No Regex matched: " + line) + log.Println("No Regex matched: " + line) } if currentMode == "insert" && valueLineRegEx.Match([]byte(line)) { @@ -92,7 +92,7 @@ func MigrateSQL(sqlContent *bufio.Scanner) (modifiedSQL2 string, err error) { currentMode = "none" currentTable = "none" } else { - // log.Println("No Regex matched: " + line) + log.Println("No Regex matched: " + line) } if currentMode == "insert" && valueLineRegEx.Match([]byte(line)) { diff --git a/utilities/utilities.go b/utilities/utilities.go index 12c0029..e95068c 100644 --- a/utilities/utilities.go +++ b/utilities/utilities.go @@ -5,11 +5,12 @@ import ( "regexp" ) +var DBTrue = true + func PrintASCII() { fmt.Println(``) fmt.Println(`P Ø N S K E L I S T E N`) fmt.Println(``) - return } func ValidatePasswordFormat(password string) (bool, string, error) {