优化删除

This commit is contained in:
kenneth 2023-12-04 02:30:27 +00:00
parent d0499bca57
commit 8f89c19e12
3 changed files with 32 additions and 6 deletions

View File

@ -34,6 +34,26 @@ func Respond(w http.ResponseWriter, message string, v any, statusCode int) {
}
}
func RespondErr(w http.ResponseWriter, message string, v any) {
rsp := response{
Success: false,
Message: message,
Data: v,
}
b, err := json.Marshal(rsp)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
_, err = w.Write(b)
if err != nil && !errors.Is(err, context.Canceled) {
log.Printf("could not write http response: %v\n", err)
}
}
func RespondJson(w http.ResponseWriter, v any) {
b, err := json.Marshal(v)
if err != nil {

View File

@ -211,19 +211,18 @@ func (server *Server) deleteVideo(w http.ResponseWriter, r *http.Request) {
var req videoDeleteRequest
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
RespondErr(w, "参数错误", nil)
return
}
log.Println(req.ID)
err := server.store.DeleteVideo(r.Context(), req.ID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
RespondErr(w, "删除失败", nil)
return
}
w.WriteHeader(http.StatusOK)
w.Write([]byte("删除成功"))
Respond(w, "删除成功", nil, http.StatusOK)
}
// transfer 视频转码

View File

@ -12,9 +12,11 @@
<div class="card-body">
<h5 class="card-title">{{.Title}}</h5>
<p class="card-text">{{.Description}}</p>
{{if not (eq .Status 1)}}
<a href="/me/videos/update/{{.ID}}" class="btn btn-warning">编辑</a>
<button id="del" data-id="{{.ID}}" class="btn btn-danger">删除</button>
{{end}}
{{if eq .Status 0}}
<button id="transfer" data-id="{{.ID}}" class="btn btn-info">转码</button>
@ -64,7 +66,12 @@
dataType: 'json',
data:JSON.stringify({"id": id}),
success: function (obj) {
$('#msg').html(obj)
if(obj.success){
alert('删除成功');
window.location.reload();
}else{
alert('删除失败');
}
},
error: function (ex) {
console.log(ex)