Skip to content

Commit

Permalink
refactor certification, caching
Browse files Browse the repository at this point in the history
  • Loading branch information
dhawton committed Nov 4, 2023
1 parent 1a6f12b commit 3587b85
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 34 deletions.
26 changes: 9 additions & 17 deletions internal/v1/certifications/certifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -85,6 +71,8 @@ func postCertifications(c *gin.Context) {
return
}

database.InvalidateCertCache()

response.Respond(c, http.StatusNoContent, nil)
}

Expand Down Expand Up @@ -144,6 +132,8 @@ func putCertifications(c *gin.Context) {
return
}

database.InvalidateCertCache()

response.Respond(c, http.StatusNoContent, nil)
}

Expand Down Expand Up @@ -188,5 +178,7 @@ func deleteCertifications(c *gin.Context) {
return
}

database.InvalidateCertCache()

response.Respond(c, http.StatusNoContent, nil)
}
52 changes: 52 additions & 0 deletions pkg/database/certifications.go
Original file line number Diff line number Diff line change
@@ -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
}
12 changes: 4 additions & 8 deletions pkg/database/dto/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}

Expand Down Expand Up @@ -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
}
Expand Down
9 changes: 0 additions & 9 deletions pkg/database/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

0 comments on commit 3587b85

Please sign in to comment.