Exemplo n.º 1
0
func handlePkgsinfoReplace(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, ps httprouter.Params) {
		name := strings.TrimLeft(ps.ByName("name"), "/")
		accept := acceptHeader(r)
		pkgsinfo, err := db.Pkgsinfo(name)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to upddate pkgsinfo: %v", err))
			return
		}

		payload := &models.PkgsInfo{}
		err = decodeRequest(r, payload)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to decode request payload: %v", err))
			return
		}

		if err := db.SavePkgsinfo(pkgsinfo); err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to save pkginfo: %v", err))
			return
		}

		pkgsinfo = payload
		pkgsinfo.Filename = name

		// manifest updated ok, respond
		respondOK(rw, pkgsinfo, accept)
	}
}
Exemplo n.º 2
0
func handlePkgsinfoCreate(db datastore.Datastore) httprouter.Handle {
	return func(rw http.ResponseWriter, r *http.Request, _ httprouter.Params) {
		accept := acceptHeader(r)
		var pkgsinfo *models.PkgsInfo
		var payload struct {
			Filename string `plist:"filename" json:"filename"`
			*models.PkgsInfo
		}
		err := decodeRequest(r, &payload)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to decode request payload: %v", err))
			return
		}

		// filename is required in the payload
		if payload.Filename == "" {
			respondError(rw, http.StatusBadRequest, accept,
				errors.New("The filename field is required to create a pkgsinfo"))
			return
		}

		// If the body contains a valid pkgsinfo, create it
		pkgsinfo = payload.PkgsInfo
		pkgsinfo.Filename = payload.Filename

		// create pkgsinfo in datastore
		_, err = db.NewPkgsinfo(payload.Filename)
		if err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to create new pkgsinfo: %v", err))
			return
		}

		// save the pkgsinfo
		if err := db.SavePkgsinfo(pkgsinfo); err != nil {
			respondError(rw, errStatus(err), accept,
				fmt.Errorf("Failed to save pkgsinfo: %v", err))
			return
		}

		respondCreated(rw, pkgsinfo, accept)
	}
}