ability to delete vehicle
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package controllers
|
package controllers
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"errors"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
"github.com/akhilrex/hammond/common"
|
"github.com/akhilrex/hammond/common"
|
||||||
@@ -14,6 +15,7 @@ func RegisterVehicleController(router *gin.RouterGroup) {
|
|||||||
router.GET("/vehicles", getAllVehicles)
|
router.GET("/vehicles", getAllVehicles)
|
||||||
router.GET("/vehicles/:id", getVehicleById)
|
router.GET("/vehicles/:id", getVehicleById)
|
||||||
router.PUT("/vehicles/:id", updateVehicle)
|
router.PUT("/vehicles/:id", updateVehicle)
|
||||||
|
router.DELETE("/vehicles/:id", deleteVehicle)
|
||||||
router.GET("/vehicles/:id/stats", getVehicleStats)
|
router.GET("/vehicles/:id/stats", getVehicleStats)
|
||||||
router.GET("/vehicles/:id/users", getVehicleUsers)
|
router.GET("/vehicles/:id/users", getVehicleUsers)
|
||||||
router.POST("/vehicles/:id/users/:subId", shareVehicle)
|
router.POST("/vehicles/:id/users/:subId", shareVehicle)
|
||||||
@@ -366,6 +368,31 @@ func getVehicleUsers(c *gin.Context) {
|
|||||||
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
|
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
func deleteVehicle(c *gin.Context) {
|
||||||
|
var searchByIdQuery models.SearchByIdQuery
|
||||||
|
|
||||||
|
if err := c.ShouldBindUri(&searchByIdQuery); err == nil {
|
||||||
|
|
||||||
|
canDelete, err := service.CanDeleteVehicle(searchByIdQuery.Id, c.MustGet("userId").(string))
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, common.NewError("shareVehicle", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !canDelete {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, common.NewError("shareVehicle", errors.New("You are not allowed to delete this vehicle.")))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = service.DeleteVehicle(searchByIdQuery.Id)
|
||||||
|
if err != nil {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, common.NewError("shareVehicle", err))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
c.JSON(http.StatusOK, gin.H{})
|
||||||
|
|
||||||
|
} else {
|
||||||
|
c.JSON(http.StatusUnprocessableEntity, common.NewValidatorError(err))
|
||||||
|
}
|
||||||
|
}
|
||||||
func shareVehicle(c *gin.Context) {
|
func shareVehicle(c *gin.Context) {
|
||||||
var searchByIdQuery models.SubItemQuery
|
var searchByIdQuery models.SubItemQuery
|
||||||
|
|
||||||
|
|||||||
@@ -62,7 +62,7 @@ func GetVehicleOwner(vehicleId string) (string, error) {
|
|||||||
if tx.Error != nil {
|
if tx.Error != nil {
|
||||||
return "", tx.Error
|
return "", tx.Error
|
||||||
}
|
}
|
||||||
return mapping.ID, nil
|
return mapping.UserID, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetVehicleUsers(vehicleId string) (*[]UserVehicle, error) {
|
func GetVehicleUsers(vehicleId string) (*[]UserVehicle, error) {
|
||||||
@@ -176,6 +176,11 @@ func GetExpenseById(id string) (*Expense, error) {
|
|||||||
return &obj, result.Error
|
return &obj, result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteVehicleById(id string) error {
|
||||||
|
|
||||||
|
result := DB.Where("id=?", id).Delete(&Vehicle{})
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
func DeleteFillupById(id string) error {
|
func DeleteFillupById(id string) error {
|
||||||
|
|
||||||
result := DB.Where("id=?", id).Delete(&Fillup{})
|
result := DB.Where("id=?", id).Delete(&Fillup{})
|
||||||
@@ -186,6 +191,16 @@ func DeleteExpenseById(id string) error {
|
|||||||
return result.Error
|
return result.Error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func DeleteFillupByVehicleId(id string) error {
|
||||||
|
|
||||||
|
result := DB.Where("vehicle_id=?", id).Delete(&Fillup{})
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
func DeleteExpenseByVehicleId(id string) error {
|
||||||
|
result := DB.Where("vehicle_id=?", id).Delete(&Expense{})
|
||||||
|
return result.Error
|
||||||
|
}
|
||||||
|
|
||||||
func GetAllQuickEntries(sorting string) (*[]QuickEntry, error) {
|
func GetAllQuickEntries(sorting string) (*[]QuickEntry, error) {
|
||||||
if sorting == "" {
|
if sorting == "" {
|
||||||
sorting = "created_at desc"
|
sorting = "created_at desc"
|
||||||
|
|||||||
@@ -42,6 +42,20 @@ func GetVehicleOwner(vehicleId string) (string, error) {
|
|||||||
func GetVehicleUsers(vehicleId string) (*[]db.UserVehicle, error) {
|
func GetVehicleUsers(vehicleId string) (*[]db.UserVehicle, error) {
|
||||||
return db.GetVehicleUsers(vehicleId)
|
return db.GetVehicleUsers(vehicleId)
|
||||||
}
|
}
|
||||||
|
func CanDeleteVehicle(vehicleId, userId string) (bool, error) {
|
||||||
|
owner, err := db.GetVehicleOwner(vehicleId)
|
||||||
|
if err != nil {
|
||||||
|
return false, err
|
||||||
|
}
|
||||||
|
return owner == userId, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteVehicle(vehicleId string) error {
|
||||||
|
db.DeleteExpenseByVehicleId(vehicleId)
|
||||||
|
db.DeleteFillupByVehicleId(vehicleId)
|
||||||
|
return db.DeleteVehicleById(vehicleId)
|
||||||
|
}
|
||||||
|
|
||||||
func ShareVehicle(vehicleId, userId string) error {
|
func ShareVehicle(vehicleId, userId string) error {
|
||||||
return db.ShareVehicle(vehicleId, userId)
|
return db.ShareVehicle(vehicleId, userId)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,8 @@ import {
|
|||||||
faDownload,
|
faDownload,
|
||||||
faEye,
|
faEye,
|
||||||
faEyeSlash,
|
faEyeSlash,
|
||||||
|
faTrash,
|
||||||
|
faShare,
|
||||||
} from '@fortawesome/free-solid-svg-icons'
|
} from '@fortawesome/free-solid-svg-icons'
|
||||||
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
|
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
|
||||||
|
|
||||||
@@ -44,7 +46,9 @@ library.add(
|
|||||||
faExclamationCircle,
|
faExclamationCircle,
|
||||||
faDownload,
|
faDownload,
|
||||||
faEye,
|
faEye,
|
||||||
faEyeSlash
|
faEyeSlash,
|
||||||
|
faTrash,
|
||||||
|
faShare
|
||||||
)
|
)
|
||||||
Vue.use(Buefy, {
|
Vue.use(Buefy, {
|
||||||
defaultIconComponent: 'vue-fontawesome',
|
defaultIconComponent: 'vue-fontawesome',
|
||||||
|
|||||||
@@ -125,6 +125,32 @@ export default {
|
|||||||
})
|
})
|
||||||
.catch((err) => console.log(err))
|
.catch((err) => console.log(err))
|
||||||
},
|
},
|
||||||
|
deleteVehicle() {
|
||||||
|
var sure = confirm(
|
||||||
|
'This will delete all the expenses and fillups related with this vehicle as well. This step cannot be reversed. Are you sure?'
|
||||||
|
)
|
||||||
|
if (sure) {
|
||||||
|
axios
|
||||||
|
.delete(`/api/vehicles/${this.vehicle.id}`)
|
||||||
|
.then((data) => {
|
||||||
|
this.$buefy.toast.open({
|
||||||
|
message: 'Vehicle Deleted Successfully',
|
||||||
|
type: 'is-success',
|
||||||
|
duration: 3000,
|
||||||
|
})
|
||||||
|
this.$router.push('/')
|
||||||
|
})
|
||||||
|
.catch((ex) => {
|
||||||
|
this.$buefy.toast.open({
|
||||||
|
duration: 5000,
|
||||||
|
message: ex.message,
|
||||||
|
position: 'is-bottom',
|
||||||
|
type: 'is-danger',
|
||||||
|
})
|
||||||
|
})
|
||||||
|
.finally(() => {})
|
||||||
|
}
|
||||||
|
},
|
||||||
addAttachment() {
|
addAttachment() {
|
||||||
if (this.file == null) {
|
if (this.file == null) {
|
||||||
return
|
return
|
||||||
@@ -219,20 +245,26 @@ export default {
|
|||||||
</p>
|
</p>
|
||||||
</div>
|
</div>
|
||||||
<div class="column is-one-third buttons has-text-centered">
|
<div class="column is-one-third buttons has-text-centered">
|
||||||
|
<b-button type="is-primary" tag="router-link" :to="`/vehicles/${vehicle.id}/fillup`">Add Fillup</b-button>
|
||||||
|
<b-button type="is-primary" tag="router-link" :to="`/vehicles/${vehicle.id}/expense`">Add Expense</b-button>
|
||||||
<b-button
|
<b-button
|
||||||
v-if="vehicle.isOwner"
|
v-if="vehicle.isOwner"
|
||||||
type="is-primary"
|
|
||||||
tag="router-link"
|
tag="router-link"
|
||||||
|
title="Edit Vehicle"
|
||||||
:to="{
|
:to="{
|
||||||
name: 'vehicle-edit',
|
name: 'vehicle-edit',
|
||||||
props: { vehicle: vehicle },
|
props: { vehicle: vehicle },
|
||||||
params: { id: vehicle.id },
|
params: { id: vehicle.id },
|
||||||
}"
|
}"
|
||||||
>Edit</b-button
|
|
||||||
>
|
>
|
||||||
<b-button type="is-primary" tag="router-link" :to="`/vehicles/${vehicle.id}/fillup`">Add Fillup</b-button>
|
<b-icon pack="fas" icon="edit" type="is-info"> </b-icon
|
||||||
<b-button type="is-primary" tag="router-link" :to="`/vehicles/${vehicle.id}/expense`">Add Expense</b-button>
|
></b-button>
|
||||||
<b-button v-if="vehicle.isOwner" type="is-primary" @click="showShareVehicleModal">Share</b-button>
|
<b-button v-if="vehicle.isOwner" title="Share vehicle" @click="showShareVehicleModal">
|
||||||
|
<b-icon pack="fas" icon="share" type="is-info"> </b-icon
|
||||||
|
></b-button>
|
||||||
|
<b-button v-if="vehicle.isOwner" title="Delete Vehicle" @click="deleteVehicle">
|
||||||
|
<b-icon pack="fas" icon="trash" type="is-danger"> </b-icon
|
||||||
|
></b-button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div v-for="(currencyLevel, index) in summaryObject" :key="index" class="level box">
|
<div v-for="(currencyLevel, index) in summaryObject" :key="index" class="level box">
|
||||||
|
|||||||
Reference in New Issue
Block a user