CreateLedger creates a new ledger record in the system. It binds the incoming JSON request to a CreateLedger object, validates it, and then creates the ledger record. If any errors occur during validation or creation, it responds with an appropriate error message. Parameters: - c: The Gin context c
(c *gin.Context)
| 36 | // - 400 Bad Request: If there's an error in binding JSON or validating the ledger. |
| 37 | // - 201 Created: If the ledger is successfully created. |
| 38 | func (a Api) CreateLedger(c *gin.Context) { |
| 39 | var newLedger model2.CreateLedger |
| 40 | if err := c.ShouldBindJSON(&newLedger); err != nil { |
| 41 | c.JSON(http.StatusBadRequest, gin.H{"errors": err.Error()}) |
| 42 | return |
| 43 | } |
| 44 | |
| 45 | err := newLedger.ValidateCreateLedger() |
| 46 | if err != nil { |
| 47 | c.JSON(http.StatusBadRequest, gin.H{"errors": err.Error()}) |
| 48 | return |
| 49 | } |
| 50 | |
| 51 | resp, err := a.ledgerforge.CreateLedger(newLedger.ToLedger()) |
| 52 | if err != nil { |
| 53 | c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) |
| 54 | return |
| 55 | } |
| 56 | |
| 57 | c.JSON(http.StatusCreated, resp) |
| 58 | } |
| 59 | |
| 60 | // GetLedger retrieves a ledger record by its ID. |
| 61 | // It extracts the ID from the route parameters and fetches the ledger record. |
nothing calls this directly
no test coverage detected