Skip to content

Commit

Permalink
user addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
Kervin Christianata committed Apr 24, 2022
1 parent 80e3e9b commit 0ed5e22
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ db/migrations/up: confirm
@echo 'Running up migrations...'
migrate -path=./migrations -database ${DB_DSN} up

## db/migrations/up: apply all up database migrations
.PHONY: db/migrations/down/1
db/migrations/down/1: confirm
@echo 'Running down migrations by 1...'
migrate -path=./migrations -database ${DB_DSN} down 1

# ==================================================================================== #
# QUALITY CONTROL
# ==================================================================================== #
Expand Down
6 changes: 6 additions & 0 deletions cmd/api/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ func (app *application) routes() http.Handler {
router.HandlerFunc(http.MethodPut, "/api/users/update/date-of-birth", app.requireAuthenticatedUser(app.updateUserDateOfBirthHandler))
router.HandlerFunc(http.MethodPut, "/api/users/update/phone-number", app.requireAuthenticatedUser(app.updateUserPhoneNumberHandler))

router.HandlerFunc(http.MethodGet, "/api/user-addresses", app.requireAuthenticatedUser(app.getUserAddressesHandler))
router.HandlerFunc(http.MethodPost, "/api/user-addresses", app.requireAuthenticatedUser(app.createUserAddressHandler))
router.HandlerFunc(http.MethodPut, "/api/user-addresses/:id", app.requireAuthenticatedUser(app.updateUserAddressHandler))
router.HandlerFunc(http.MethodPut, "/api/user-addresses/:id/is-main", app.requireAuthenticatedUser(app.updateMainUserAddressHandler))
router.HandlerFunc(http.MethodDelete, "/api/user-addresses/:id", app.requireAuthenticatedUser(app.deleteUserAddressHandler))

// Tokens
router.HandlerFunc(http.MethodPost, "/api/tokens/authentication", app.createAuthenticationTokenHandler)
router.HandlerFunc(http.MethodPost, "/api/tokens/activation", app.createActivationTokenHandler)
Expand Down
215 changes: 215 additions & 0 deletions cmd/api/user_addresses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,215 @@
package main

import (
"errors"
"net/http"

"github.com/kervinch/internal/data"
"github.com/kervinch/internal/validator"
)

// ====================================================================================
// Business Handlers
// ====================================================================================

func (app *application) getUserAddressesHandler(w http.ResponseWriter, r *http.Request) {
user := app.contextGetUser(r)

userAddresses, err := app.gorm.UserAddresses.GetAPI(user)

if err != nil {
app.serverErrorResponse(w, r, err)
return
}

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), userAddresses, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}

func (app *application) createUserAddressHandler(w http.ResponseWriter, r *http.Request) {
var input struct {
Name string `json:"name"`
Receiver string `json:"receiver"`
PhoneNumber string `json:"phone_number"`
City string `json:"city"`
PostalCode string `json:"postal_code"`
Address string `json:"address"`
}

err := app.readJSON(w, r, &input)
if err != nil {
app.badRequestResponse(w, r, err)
return
}

user := app.contextGetUser(r)

userAddress := &data.UserAddress{
UserID: user.ID,
Name: input.Name,
Receiver: input.Receiver,
PhoneNumber: input.PhoneNumber,
City: input.City,
PostalCode: input.PostalCode,
Address: input.Address,
IsMain: false,
}

v := validator.New()

if data.ValidateUserAddress(v, userAddress); !v.Valid() {
app.failedValidationResponse(w, r, v.Errors)
return
}

err = app.gorm.UserAddresses.Insert(userAddress)
if err != nil {
app.serverErrorResponse(w, r, err)
return
}

err = app.writeJSON(w, http.StatusCreated, http.StatusText(http.StatusCreated), userAddress, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}

func (app *application) updateUserAddressHandler(w http.ResponseWriter, r *http.Request) {
user := app.contextGetUser(r)

id, err := app.readIDParam(r)
if err != nil {
app.notFoundResponse(w, r)
return
}

userAddress, err := app.gorm.UserAddresses.Get(id, user)
if err != nil {
switch {
case errors.Is(err, data.ErrRecordNotFound):
app.notFoundResponse(w, r)
default:
app.serverErrorResponse(w, r, err)
}
return
}

var input struct {
Name string `json:"name"`
Receiver string `json:"receiver"`
PhoneNumber string `json:"phone_number"`
City string `json:"city"`
PostalCode string `json:"postal_code"`
Address string `json:"address"`
}

err = app.readJSON(w, r, &input)
if err != nil {
app.badRequestResponse(w, r, err)
return
}

userAddress.Name = input.Name
userAddress.Receiver = input.Receiver
userAddress.PhoneNumber = input.PhoneNumber
userAddress.City = input.City
userAddress.PostalCode = input.PostalCode
userAddress.Address = input.Address

v := validator.New()

if data.ValidateUserAddress(v, userAddress); !v.Valid() {
app.failedValidationResponse(w, r, v.Errors)
return
}

err = app.gorm.UserAddresses.Update(userAddress, user)
if err != nil {
app.serverErrorResponse(w, r, err)
return
}

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), userAddress, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}

func (app *application) updateMainUserAddressHandler(w http.ResponseWriter, r *http.Request) {
user := app.contextGetUser(r)

id, err := app.readIDParam(r)
if err != nil {
app.notFoundResponse(w, r)
return
}

userAddress, err := app.gorm.UserAddresses.Get(id, user)
if err != nil {
switch {
case errors.Is(err, data.ErrRecordNotFound):
app.notFoundResponse(w, r)
default:
app.serverErrorResponse(w, r, err)
}
return
}

var input struct {
IsMain bool `json:"is_main"`
}

err = app.readJSON(w, r, &input)
if err != nil {
app.badRequestResponse(w, r, err)
return
}

userAddress.IsMain = input.IsMain

v := validator.New()

if data.ValidateUserAddress(v, userAddress); !v.Valid() {
app.failedValidationResponse(w, r, v.Errors)
return
}

err = app.gorm.UserAddresses.UpdateMain(userAddress, user)
if err != nil {
app.serverErrorResponse(w, r, err)
return
}

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), userAddress, nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}

func (app *application) deleteUserAddressHandler(w http.ResponseWriter, r *http.Request) {
user := app.contextGetUser(r)

id, err := app.readIDParam(r)
if err != nil {
app.notFoundResponse(w, r)
return
}

err = app.gorm.UserAddresses.Delete(id, user.ID)
if err != nil {
switch {
case errors.Is(err, data.ErrRecordNotFound):
app.notFoundResponse(w, r)
default:
app.serverErrorResponse(w, r, err)
}
return
}

err = app.writeJSON(w, http.StatusOK, http.StatusText(http.StatusOK), "user address successfully deleted", nil)
if err != nil {
app.serverErrorResponse(w, r, err)
}
}
2 changes: 2 additions & 0 deletions internal/data/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Gorm struct {
Blogs BlogModel
BlogCategories BlogCategoryModel
ProductCategories ProductCategoryModel
UserAddresses UserAddressModel
}

func NewModels(db *sql.DB) Models {
Expand All @@ -47,6 +48,7 @@ func GormModels(db *gorm.DB) Gorm {
Blogs: BlogModel{DB: db},
BlogCategories: BlogCategoryModel{DB: db},
ProductCategories: ProductCategoryModel{DB: db},
UserAddresses: UserAddressModel{DB: db},
}
}

Expand Down
Loading

0 comments on commit 0ed5e22

Please sign in to comment.