Skip to content

Commit

Permalink
changing accession route for new records
Browse files Browse the repository at this point in the history
  • Loading branch information
dmnyu committed May 15, 2024
1 parent 2f0d7a6 commit b69cada
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 36 deletions.
41 changes: 13 additions & 28 deletions controllers/accessionsController.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controllers

import (
"fmt"
"log"
"net/http"
"strconv"
"time"
Expand Down Expand Up @@ -124,7 +125,7 @@ func NewAccession(c *gin.Context) {
return
}

resourceID, err := strconv.Atoi(c.Param("id"))
resourceID, err := strconv.Atoi(c.Query("resource_id"))
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
Expand Down Expand Up @@ -160,42 +161,26 @@ func CreateAccession(c *gin.Context) {
return
}

userId, err := getUserkey(c)
userID, err := getUserkey(c)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}

accession.CreatedAt = time.Now()
accession.CreatedBy = userId
accession.CreatedBy = userID
accession.UpdatedAt = time.Now()
accession.UpdatedBy = userId

c.JSON(200, accession)

/*
log.Println("Controller collection ID", accession.CollectionID)
userId, err := getUserkey(c)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}
accession.UpdatedBy = userID

accession.CreatedAt = time.Now()
accession.CreatedBy = userId
accession.UpdatedAt = time.Now()
accession.UpdatedBy = userId
accessionID, err := database.InsertAccession(&accession)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}
log.Println("CONTROLLER BEFORE:", accession.CollectionID)
accessionID, err := database.InsertAccession(&accession)
if err != nil {
c.JSON(http.StatusBadRequest, err.Error())
return
}
log.Println("CONTROLLER AFTER:", accession.CollectionID)

c.Redirect(302, fmt.Sprintf("/accessions/%d/show", accessionID))
*/
c.Redirect(302, fmt.Sprintf("/accessions/%d/show", accessionID))

}

Expand Down
4 changes: 4 additions & 0 deletions database/accessions.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package database

import (
"log"

"github.com/nyudlts/go-medialog/models"
"github.com/nyudlts/go-medialog/utils"
"gorm.io/gorm/clause"
Expand Down Expand Up @@ -40,9 +42,11 @@ func FindPaginatedAccessions(pagination utils.Pagination) ([]models.Accession, e
}

func InsertAccession(accession *models.Accession) (uint, error) {
log.Println("DATABASE BEFORE:", accession.CollectionID)
if err := db.Create(accession).Error; err != nil {
return 0, err
}
log.Println("DATABASE AFTER", accession.CollectionID)
return accession.ID, nil
}

Expand Down
2 changes: 1 addition & 1 deletion medialog.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func main() {
router.SetTrustedProxies([]string{"127.0.0.1"})

//connect the database
if err := database.ConnectDatabase(filepath.Join("database", "medialog.db")); err != nil {
if err := database.ConnectDatabase(filepath.Join("database", "medialog-test.db")); err != nil {
os.Exit(2)
}

Expand Down
2 changes: 1 addition & 1 deletion migration/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func createAdminUser() {
hash := sha512.Sum512([]byte(password + user.Salt))
user.EncryptedPassword = hex.EncodeToString(hash[:])

if err := database.InsertUser(&user); err != nil {
if _, err := database.InsertUser(&user); err != nil {
panic(err)
}

Expand Down
2 changes: 1 addition & 1 deletion routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func LoadRoutes(router *gin.Engine) {

//Accessions Group
accessionsRoutes := router.Group("/accessions")
accessionsRoutes.GET(":id/new", func(c *gin.Context) { controllers.NewAccession(c) })
accessionsRoutes.GET("new", func(c *gin.Context) { controllers.NewAccession(c) })
accessionsRoutes.POST("", func(c *gin.Context) { controllers.CreateAccession(c) })
accessionsRoutes.GET("", func(c *gin.Context) { controllers.GetAccessions(c) })
accessionsRoutes.GET(":id/show", func(c *gin.Context) { controllers.GetAccession(c) })
Expand Down
5 changes: 1 addition & 4 deletions templates/accessions/accessions-new.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,7 @@ <h5 class="card-title">Create an Accession</h5>
<label for="accession_num">Accession Call Number</label>
<input type="text" name="accession_num" id="accession_num" />
</div>
<div class="form-group">
<label for="accession_num">Accession Call Number</label>
<input type="number" name="collection_id" id="collectionID" value="{{.resource.ID}}" />
</div>
<input type="hidden" name="collection_id" id="collection_id" value="{{ .resource.ID }}" />
<input type="hidden" name="repository_id" id="repository_id" value="{{ .repository.ID }}">
<input class="btn btn-primary" type="submit">
</form>
Expand Down
2 changes: 1 addition & 1 deletion templates/resources/resources-show.html
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ <h5 class="card-title">Accessions</h5>
</tbody>
</table>
<div class="row">
<a href="/accessions/{{ .resource.ID }}/new" class="btn btn-primary">Add Accession</a>
<a href="/accessions/new?resource_id={{.resource.ID}}" class="btn btn-primary">Add Accession</a>
</div>
</div>
</div>
Expand Down

0 comments on commit b69cada

Please sign in to comment.