Goweb开发之Iris框架实战:删除记录功能

上节内容我们分析了添加商铺记录的功能,今天我们继续来开发删除记录功能。在本节内容中,我们演示删除商家列表和食品列表记录。

接口分析

在小幺鸡文档或者浏览器中开发者工具中,我们都可以找到删除商家记录和删除食品列表记录的接口,具体的接口功能如下:

删除商家

  • 接口:/shopping/restaurant/1
  • 类型:Delete
  • 描述:该接口用于删除指定的商家记录,接口请求最后的数字即表示要删除的商家的编号

删除食品

  • 接口:/shopping/v2/food/1
  • 类型:DELETE
  • 描述:该接口用于删除指定的食品记录,接口请求最后的数字即表示要删除的食品记录的编号。

路由解析

我们在控制器中,采用配置路由的方式来实现该删除功能的路由解析,之前讲过的BeforeActivation方法,会在控制器Controller中解析请求时首先执行,并进行相应的方法解析,我们定义如下的删除方法解析:

func (cc *CategoryController) BeforeActivation(a mvc.BeforeActivation) {
​
    //通过商铺Id获取对应的食品种类
    a.Handle("GET", "/getcategory/{shopId}", "GetCategoryByShopId")
​
    //获取全部的食品种类
    a.Handle("GET", "/v2/restaurant/category", "GetAllCategory")
​
    //添加商铺记录
    a.Handle("POST", "/addShop", "PostAddShop")
​
    //删除商铺记录
    a.Handle("DELETE", "/restaurant/{restaurant_id}", "DeleteRestaurant")
​
    //删除食品记录
    a.Handle("DELETE", "/v2/food/{food_id}", "DeleteFood")
}
​

定义完成如上的请求url和对应的方法后,完整的controller中实现请求处理的两个方法如下:

func (cc *CategoryController) DeleteRestaurant() mvc.Result {
    restaurant_id := cc.Ctx.Params().Get("restaurant_id")
​
    shopId, err := strconv.Atoi(restaurant_id)
​
    if err != nil {
        return mvc.Response{
            Object: map[string]interface{}{
                "status":  utils.RECODE_FAIL,
                "type":    utils.RESPMSG_HASNOACCESS,
                "message": utils.Recode2Text(utils.RESPMSG_HASNOACCESS),
            },
        }
    }
    delete := cc.Service.DeleteShop(shopId)
    if !delete {
        return mvc.Response{
            Object: map[string]interface{}{
                "status":  utils.RECODE_FAIL,
                "type":    utils.RESPMSG_HASNOACCESS,
                "message": utils.Recode2Text(utils.RESPMSG_HASNOACCESS),
            },
        }
    } else {
        return mvc.Response{
            Object: map[string]interface{}{
                "status":  utils.RECODE_OK,
                "type":    utils.RESPMSG_SUCCESS_DELETESHOP,
                "message": utils.Recode2Text(utils.RESPMSG_SUCCESS_DELETESHOP),
            },
        }
    }
}
​
func (cc *CategoryController) DeleteFood() mvc.Result {
    food_id := cc.Ctx.Params().Get("food_id")
​
    foodID, err := strconv.Atoi(food_id)
​
    if err != nil {
        return mvc.Response{
            Object: map[string]interface{}{
                "status":  utils.RECODE_FAIL,
                "type":    utils.RESPMSG_HASNOACCESS,
                "message": utils.Recode2Text(utils.RESPMSG_HASNOACCESS),
            },
        }
    }
    delete := cc.Service.DeleteFood(foodID)
    if !delete {
        return mvc.Response{
            Object: map[string]interface{}{
                "status":  utils.RECODE_FAIL,
                "type":    utils.RESPMSG_HASNOACCESS,
                "message": utils.Recode2Text(utils.RESPMSG_HASNOACCESS),
            },
        }
    } else {
        return mvc.Response{
            Object: map[string]interface{}{
                "status":  utils.RECODE_OK,
                "type":    utils.RESPMSG_SUCCESS_FOODDELE,
                "message": utils.Recode2Text(utils.RESPMSG_SUCCESS_FOODDELE),
            },
        }
    }
}
​

Service提供更新数据库功能

如上已经完成了路由的解析设置和请求处理方法,我们需要在service中实现更新数据库的方法,删除食品并更新数据库和删除商铺并更新数据库的两个方法如下:

func (cs *categoryService) DeleteShop(restaurantId int) bool {
    shop := model.Shop{ShopId: restaurantId, Dele: 1}
    _, err := cs.Engine.Where(" shop_id = ? ", restaurantId).Cols("dele").Update(&shop)
    if err != nil {
        iris.New().Logger().Info(err.Error())
    }
    return err == nil
}
​
func (cs *categoryService) DeleteFood(foodId int) bool {
    food := model.Food{Id: int64(foodId), DelFlag: 1}
    _, err := cs.Engine.Where(" id = ? ", foodId).Cols("del_flag").Update(&food)
    if err != nil {
        iris.New().Logger().Info(err.Error())
    }
    return err == nil
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值