github.com/bitcubate/cryptojournal@v1.2.5-0.20171102134152-f578b3d788ab/src/lib/templates/fragmenta_resources/actions/destroy.go.tmpl (about)

     1  package [[ .fragmenta_resource ]]actions
     2  
     3  import (
     4  	"net/http"
     5  
     6  	"github.com/fragmenta/auth/can"
     7  	"github.com/fragmenta/mux"
     8  	"github.com/fragmenta/server"
     9  
    10  	"github.com/bitcubate/cryptojournal/src/lib/session"
    11  	"github.com/bitcubate/cryptojournal/src/[[ .fragmenta_resources ]]"
    12  )
    13  
    14  // HandleDestroy responds to /[[ .fragmenta_resources ]]/n/destroy by deleting the [[ .fragmenta_resource ]].
    15  func HandleDestroy(w http.ResponseWriter, r *http.Request) error {
    16  
    17  	// Fetch the  params
    18  	params, err := mux.Params(r)
    19  	if err != nil {
    20  		return server.InternalError(err)
    21  	}
    22  	
    23  	// Find the [[ .fragmenta_resource ]]
    24  	[[ .fragmenta_resource ]], err := [[ .fragmenta_resources ]].Find(params.GetInt([[ .fragmenta_resources ]].KeyName))
    25  	if err != nil {
    26  		return server.NotFoundError(err)
    27  	}
    28  
    29  	// Check the authenticity token
    30  	err = session.CheckAuthenticity(w, r)
    31  	if err != nil {
    32  		return err
    33  	}
    34  
    35  	// Authorise destroy [[ .fragmenta_resource ]]
    36  	err = can.Destroy([[ .fragmenta_resource ]], session.CurrentUser(w, r))
    37  	if err != nil {
    38  		return server.NotAuthorizedError(err)
    39  	}
    40  
    41  	// Destroy the [[ .fragmenta_resource ]]
    42  	[[ .fragmenta_resource ]].Destroy()
    43  
    44  	// Redirect to [[ .fragmenta_resources ]] root
    45  	return server.Redirect(w, r, [[ .fragmenta_resource ]].IndexURL())
    46  
    47  }