ability to delete vehicle
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"net/http"
|
||||
|
||||
"github.com/akhilrex/hammond/common"
|
||||
@@ -14,6 +15,7 @@ func RegisterVehicleController(router *gin.RouterGroup) {
|
||||
router.GET("/vehicles", getAllVehicles)
|
||||
router.GET("/vehicles/:id", getVehicleById)
|
||||
router.PUT("/vehicles/:id", updateVehicle)
|
||||
router.DELETE("/vehicles/:id", deleteVehicle)
|
||||
router.GET("/vehicles/:id/stats", getVehicleStats)
|
||||
router.GET("/vehicles/:id/users", getVehicleUsers)
|
||||
router.POST("/vehicles/:id/users/:subId", shareVehicle)
|
||||
@@ -366,6 +368,31 @@ func getVehicleUsers(c *gin.Context) {
|
||||
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) {
|
||||
var searchByIdQuery models.SubItemQuery
|
||||
|
||||
|
||||
@@ -62,7 +62,7 @@ func GetVehicleOwner(vehicleId string) (string, error) {
|
||||
if tx.Error != nil {
|
||||
return "", tx.Error
|
||||
}
|
||||
return mapping.ID, nil
|
||||
return mapping.UserID, nil
|
||||
}
|
||||
|
||||
func GetVehicleUsers(vehicleId string) (*[]UserVehicle, error) {
|
||||
@@ -176,6 +176,11 @@ func GetExpenseById(id string) (*Expense, 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 {
|
||||
|
||||
result := DB.Where("id=?", id).Delete(&Fillup{})
|
||||
@@ -186,6 +191,16 @@ func DeleteExpenseById(id string) 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) {
|
||||
if sorting == "" {
|
||||
sorting = "created_at desc"
|
||||
|
||||
@@ -42,6 +42,20 @@ func GetVehicleOwner(vehicleId string) (string, error) {
|
||||
func GetVehicleUsers(vehicleId string) (*[]db.UserVehicle, error) {
|
||||
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 {
|
||||
return db.ShareVehicle(vehicleId, userId)
|
||||
}
|
||||
|
||||
@@ -18,6 +18,8 @@ import {
|
||||
faDownload,
|
||||
faEye,
|
||||
faEyeSlash,
|
||||
faTrash,
|
||||
faShare,
|
||||
} from '@fortawesome/free-solid-svg-icons'
|
||||
import { FontAwesomeIcon } from '@fortawesome/vue-fontawesome'
|
||||
|
||||
@@ -44,7 +46,9 @@ library.add(
|
||||
faExclamationCircle,
|
||||
faDownload,
|
||||
faEye,
|
||||
faEyeSlash
|
||||
faEyeSlash,
|
||||
faTrash,
|
||||
faShare
|
||||
)
|
||||
Vue.use(Buefy, {
|
||||
defaultIconComponent: 'vue-fontawesome',
|
||||
|
||||
@@ -125,6 +125,32 @@ export default {
|
||||
})
|
||||
.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() {
|
||||
if (this.file == null) {
|
||||
return
|
||||
@@ -219,20 +245,26 @@ export default {
|
||||
</p>
|
||||
</div>
|
||||
<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
|
||||
v-if="vehicle.isOwner"
|
||||
type="is-primary"
|
||||
tag="router-link"
|
||||
title="Edit Vehicle"
|
||||
:to="{
|
||||
name: 'vehicle-edit',
|
||||
props: { vehicle: vehicle },
|
||||
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-button type="is-primary" tag="router-link" :to="`/vehicles/${vehicle.id}/expense`">Add Expense</b-button>
|
||||
<b-button v-if="vehicle.isOwner" type="is-primary" @click="showShareVehicleModal">Share</b-button>
|
||||
<b-icon pack="fas" icon="edit" type="is-info"> </b-icon
|
||||
></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 v-for="(currencyLevel, index) in summaryObject" :key="index" class="level box">
|
||||
|
||||
Reference in New Issue
Block a user