diff --git a/main.go b/main.go index 4fd6403..596cf3a 100644 --- a/main.go +++ b/main.go @@ -51,6 +51,7 @@ func main() { }) // API to interact with resumés + router.GET("/resumes/latest", getLatestResume) router.GET("/resumes", getResumes) router.GET("/resumes/:id", getResumeByID) router.PATCH("/resumes/:id", updateResumeByID) @@ -102,6 +103,18 @@ func setupDB() error { return nil } +func getLatestResume(c *gin.Context) { + var latestResume Resume + + // Order by year in descending order and then by the latest modification time + if err := db.Order("year desc, updated_at desc").First(&latestResume).Error; err != nil { + c.IndentedJSON(http.StatusNotFound, gin.H{"message": "No resumes found"}) + return + } + + c.IndentedJSON(http.StatusOK, latestResume) +} + func getResumes(c *gin.Context) { var resumes []Resume db.Find(&resumes) diff --git a/openapispec.yaml b/openapispec.yaml index 7100d7f..728e672 100644 --- a/openapispec.yaml +++ b/openapispec.yaml @@ -138,6 +138,32 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + '/resumes/latest': + get: + operationId: getLatestResume + summary: Get the most recent resumé + description: API to get the most recent resumé by year. In case of a tie, returns the most recently modified one. + tags: + - resumes + responses: + '200': + description: Successful response + content: + application/json: + schema: + $ref: '#/components/schemas/ResumeResponse' + '404': + description: No resumes found + content: + application/json: + schema: + $ref: '#/components/schemas/ErrorResponse' + default: + description: Unexpected error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' components: schemas: Resume: