SqlX working
This commit is contained in:
parent
b5f71890a6
commit
8bf126279c
@ -2,7 +2,6 @@ package controllers
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/gin-gonic/gin/binding"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"web-service-gin/internal/forms"
|
"web-service-gin/internal/forms"
|
||||||
models2 "web-service-gin/internal/models"
|
models2 "web-service-gin/internal/models"
|
||||||
@ -24,7 +23,7 @@ func NewAlbum(albumService models2.AlbumService) *Album {
|
|||||||
// Post will create a new album from the given data, if the form is valid.
|
// Post will create a new album from the given data, if the form is valid.
|
||||||
func (p *Album) Post(c *gin.Context) {
|
func (p *Album) Post(c *gin.Context) {
|
||||||
var form forms.CreateAlbum
|
var form forms.CreateAlbum
|
||||||
if c.ShouldBindWith(&form, binding.JSON) != nil {
|
if c.ShouldBindJSON(&form) != nil {
|
||||||
// TODO: Give a better error message.
|
// TODO: Give a better error message.
|
||||||
c.JSON(
|
c.JSON(
|
||||||
http.StatusNotAcceptable,
|
http.StatusNotAcceptable,
|
||||||
@ -58,7 +57,7 @@ func (p *Album) Post(c *gin.Context) {
|
|||||||
// Put will perform an update of a album.
|
// Put will perform an update of a album.
|
||||||
func (p *Album) Put(c *gin.Context) {
|
func (p *Album) Put(c *gin.Context) {
|
||||||
var form forms.CreateAlbum
|
var form forms.CreateAlbum
|
||||||
if err := c.ShouldBindWith(&form, binding.JSON); err != nil {
|
if err := c.ShouldBindJSON(&form); err != nil {
|
||||||
// TODO: Give a better error message.
|
// TODO: Give a better error message.
|
||||||
c.JSON(
|
c.JSON(
|
||||||
http.StatusNotAcceptable,
|
http.StatusNotAcceptable,
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
package forms
|
package forms
|
||||||
|
|
||||||
type CreateAlbum struct {
|
type CreateAlbum struct {
|
||||||
ID *int64 `form:"id" json:"id" binding:"required"`
|
ID *int64 `form:"id" json:"id"`
|
||||||
Title *string `form:"title" json:"title" binding:"required"`
|
Title *string `form:"title" json:"title" binding:"required"`
|
||||||
Artist *string `form:"artist" json:"artist" binding:"required"`
|
Artist *string `form:"artist" json:"artist" binding:"required"`
|
||||||
Price *float64 `form:"price" json:"price" binding:"required"`
|
Price *float64 `form:"price" json:"price" binding:"required"`
|
||||||
|
@ -3,6 +3,7 @@ package sql
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
"strconv"
|
||||||
"web-service-gin/internal/forms"
|
"web-service-gin/internal/forms"
|
||||||
models2 "web-service-gin/internal/models"
|
models2 "web-service-gin/internal/models"
|
||||||
)
|
)
|
||||||
@ -37,24 +38,28 @@ func NewAlbumService(conn *sqlx.DB) (*AlbumService, error) {
|
|||||||
|
|
||||||
// Create will try to add the album to the DB.
|
// Create will try to add the album to the DB.
|
||||||
func (s *AlbumService) Create(form *forms.CreateAlbum) (*models2.Album, error) {
|
func (s *AlbumService) Create(form *forms.CreateAlbum) (*models2.Album, error) {
|
||||||
q := `
|
q := `INSERT INTO albums(title, artist, price) VALUES (?, ?, ?);`
|
||||||
INSERT INTO albums(title, artist, price)
|
|
||||||
VALUES (?, ?, ?)
|
|
||||||
RETURNING *;`
|
|
||||||
|
|
||||||
var output models2.Album
|
result, err := s.conn.Exec(
|
||||||
err := s.conn.Get(
|
|
||||||
&output,
|
|
||||||
q,
|
q,
|
||||||
*form.Title,
|
*form.Title,
|
||||||
*form.Artist,
|
*form.Artist,
|
||||||
*form.Price,
|
*form.Price,
|
||||||
)
|
)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
return &output, nil
|
|
||||||
|
id, err := result.LastInsertId()
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
album, err := s.GetByID(strconv.FormatUint(uint64(id), 10))
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return album, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update will replace the values of the give album with those provided.
|
// Update will replace the values of the give album with those provided.
|
||||||
|
@ -29,3 +29,10 @@ POST http://localhost:8080/albums
|
|||||||
Content-Type: application/json
|
Content-Type: application/json
|
||||||
|
|
||||||
{"title": "Sarah Vaughan and Clifford Brown", "artist": "Sarah Vaughan", "price": 39.99}
|
{"title": "Sarah Vaughan and Clifford Brown", "artist": "Sarah Vaughan", "price": 39.99}
|
||||||
|
|
||||||
|
|
||||||
|
###
|
||||||
|
POST http://localhost:8080/albums
|
||||||
|
Content-Type: application/json
|
||||||
|
|
||||||
|
{"title": "2Pacalypse", "artist": "2pac", "price": 75.23}
|
||||||
|
Loading…
Reference in New Issue
Block a user