From 3587b85ff9222530a29cb458d2d24569e5110406 Mon Sep 17 00:00:00 2001 From: Daniel Hawton Date: Sat, 4 Nov 2023 15:33:12 -0600 Subject: [PATCH] refactor certification, caching --- internal/v1/certifications/certifications.go | 26 ++++------ pkg/database/certifications.go | 52 ++++++++++++++++++++ pkg/database/dto/user.go | 12 ++--- pkg/database/models.go | 9 ---- 4 files changed, 65 insertions(+), 34 deletions(-) create mode 100644 pkg/database/certifications.go diff --git a/internal/v1/certifications/certifications.go b/internal/v1/certifications/certifications.go index 679378d2..17bf6800 100644 --- a/internal/v1/certifications/certifications.go +++ b/internal/v1/certifications/certifications.go @@ -31,30 +31,15 @@ type CertificationDTO struct { Name string `json:"name"` } -type CertificationResponseDTO struct { - Certificiations []string `json:"certifications"` -} - // Get certification types // @Summary Get certification types // @Description Get certification types // @Tags certifications -// @Success 200 {object} CertificationResponseDTO +// @Success 200 {object} []string // @Failure 500 {object} response.R // @Router /v1/certifications [get] func getCertifications(c *gin.Context) { - var certifications []models.Certification - if err := database.DB.Find(&certifications).Error; err != nil { - response.RespondError(c, http.StatusInternalServerError, "Internal Server Error") - return - } - - var certificationNames []string - for _, certification := range certifications { - certificationNames = append(certificationNames, certification.Name) - } - - response.Respond(c, http.StatusOK, CertificationResponseDTO{Certificiations: certificationNames}) + response.Respond(c, http.StatusOK, database.GetCertifications()) } // Create a new certification type @@ -69,6 +54,7 @@ func getCertifications(c *gin.Context) { // @Router /v1/certifications [post] func postCertifications(c *gin.Context) { var certificationDTO CertificationDTO + if err := c.ShouldBind(&certificationDTO); err != nil { response.RespondError(c, http.StatusBadRequest, "Bad Request") return @@ -85,6 +71,8 @@ func postCertifications(c *gin.Context) { return } + database.InvalidateCertCache() + response.Respond(c, http.StatusNoContent, nil) } @@ -144,6 +132,8 @@ func putCertifications(c *gin.Context) { return } + database.InvalidateCertCache() + response.Respond(c, http.StatusNoContent, nil) } @@ -188,5 +178,7 @@ func deleteCertifications(c *gin.Context) { return } + database.InvalidateCertCache() + response.Respond(c, http.StatusNoContent, nil) } diff --git a/pkg/database/certifications.go b/pkg/database/certifications.go new file mode 100644 index 00000000..41ec7c0e --- /dev/null +++ b/pkg/database/certifications.go @@ -0,0 +1,52 @@ +/* + * Copyright ADH Partnership + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package database + +import "github.com/adh-partnership/api/pkg/database/models" + +var certCache []string = nil + +func GetCertifications() []string { + if certCache == nil { + certCache = make([]string, 0) + certs := []models.Certification{} + if err := DB.Model(&models.Certification{}).Find(&certs).Error; err != nil { + log.Errorf("Error getting certifications: %v", err) + return nil + } + for _, cert := range certs { + certCache = append(certCache, cert.Name) + } + } + + return certCache +} + +func InvalidateCertCache() { + certCache = nil +} + +func ValidCertification(key string) bool { + certifications := GetCertifications() + for _, cert := range certifications { + if cert == key { + return true + } + } + + return false +} diff --git a/pkg/database/dto/user.go b/pkg/database/dto/user.go index 42a50165..1cdecd27 100644 --- a/pkg/database/dto/user.go +++ b/pkg/database/dto/user.go @@ -109,13 +109,9 @@ func ConvUserToUserResponse(user *models.User) *UserResponse { } // Fill in other certifications with "none" - mainCerts := []models.Certification{} - if err := database.DB.Model(&models.Certification{}).Find(&mainCerts).Error; err != nil { - return nil - } - for _, c := range mainCerts { - if _, ok := certs[c.Name]; !ok { - certs[c.Name] = "none" + for _, c := range database.GetCertifications() { + if _, ok := certs[c]; !ok { + certs[c] = "none" } } @@ -190,7 +186,7 @@ func PatchUserFromUserResponse(user *models.User, userResponse UserResponseAdmin errs = append(errs, err.Error()) } else { for certName, certValue := range userResponse.Certifications { - if ok, _ := database.ValidCertification(certName); !ok { + if !database.ValidCertification(certName) { errs = append(errs, ErrInvalidCertification) continue } diff --git a/pkg/database/models.go b/pkg/database/models.go index 19554f12..fd174c96 100644 --- a/pkg/database/models.go +++ b/pkg/database/models.go @@ -334,12 +334,3 @@ func FindAPIKey(key string) (*models.APIKeys, error) { return apikey, nil } - -func ValidCertification(key string) (bool, error) { - cert := &models.Certification{} - if err := DB.Where(models.Certification{Name: key}).First(cert).Error; err != nil { - return false, err - } - - return true, nil -}