// updateInvestigator updates the status of an investigator in database
func updateInvestigator(respWriter http.ResponseWriter, request *http.Request) {
	var err error
	opid := getOpID(request)
	loc := fmt.Sprintf("%s%s", ctx.Server.Host, request.URL.String())
	resource := cljs.New(loc)
	defer func() {
		if e := recover(); e != nil {
			emsg := fmt.Sprintf("%v", e)
			ctx.Channels.Log <- mig.Log{OpID: opid, Desc: emsg}.Err()
			resource.SetError(cljs.Error{Code: fmt.Sprintf("%.0f", opid), Message: emsg})
			respond(500, resource, respWriter, request)
		}
		ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving updateInvestigator()"}.Debug()
	}()
	var inv mig.Investigator
	err = request.ParseForm()
	if err != nil {
		panic(err)
	}
	iid := request.FormValue("id")
	if iid == "" {
		panic("Investigator ID must not be empty")
	}
	inv.ID, err = strconv.ParseFloat(iid, 64)
	if err != nil {
		panic(err)
	}
	inv.Status = request.FormValue("status")
	if inv.Status == "" {
		panic("Investigator status must not be empty")
	}
	// create the investigator in database
	err = ctx.DB.UpdateInvestigatorStatus(inv)
	if err != nil {
		panic(err)
	}
	ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("Investigator %.0f status changed to %s", inv.ID, inv.Status)}
	err = resource.AddItem(cljs.Item{
		Href: fmt.Sprintf("%s/investigator?investigatorid=%.0f", ctx.Server.BaseURL, inv.ID),
		Data: []cljs.Data{{Name: "Investigator ID " + fmt.Sprintf("%.0f", inv.ID), Value: inv}},
	})
	respond(200, resource, respWriter, request)
}
示例#2
0
// updateInvestigator updates the status of an investigator in database
func updateInvestigator(respWriter http.ResponseWriter, request *http.Request) {
	var err error
	opid := getOpID(request)
	loc := fmt.Sprintf("%s%s", ctx.Server.Host, request.URL.String())
	resource := cljs.New(loc)
	defer func() {
		if e := recover(); e != nil {
			emsg := fmt.Sprintf("%v", e)
			ctx.Channels.Log <- mig.Log{OpID: opid, Desc: emsg}.Err()
			resource.SetError(cljs.Error{Code: fmt.Sprintf("%.0f", opid), Message: emsg})
			respond(http.StatusInternalServerError, resource, respWriter, request)
		}
		ctx.Channels.Log <- mig.Log{OpID: opid, Desc: "leaving updateInvestigator()"}.Debug()
	}()
	var inv mig.Investigator
	err = request.ParseForm()
	if err != nil {
		panic(err)
	}
	iid := request.FormValue("id")
	if iid == "" {
		panic("Investigator ID must not be empty")
	}
	inv.ID, err = strconv.ParseFloat(iid, 64)
	if err != nil {
		panic(err)
	}
	inv.Status = request.FormValue("status")
	isadm := request.FormValue("isadmin")
	if inv.Status == "" && isadm == "" {
		panic("No updates to the investigator were specified")
	}
	if inv.Status != "" {
		// update the investigator status in database
		err = ctx.DB.UpdateInvestigatorStatus(inv)
		if err != nil {
			panic(err)
		}
		ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("Investigator %.0f status changed to %s", inv.ID, inv.Status)}
	} else {
		switch isadm {
		case "true":
			inv.IsAdmin = true
		case "false":
			inv.IsAdmin = false
			// If the request is to disable the admin flag, make sure we
			// are not disabling the only remaining admin
			var cnt int
			cnt, err = ctx.DB.CountOtherAdminInvestigators(inv)
			if err != nil {
				panic(err)
			}
			if cnt < 1 {
				resource.SetError(cljs.Error{
					Code:    fmt.Sprintf("%.0f", opid),
					Message: "Will not disable last remaining administrator"})
				respond(http.StatusBadRequest, resource, respWriter, request)
				return
			}
		default:
			panic("invalid value for isadmin")
		}
		err = ctx.DB.UpdateInvestigatorAdmin(inv)
		if err != nil {
			panic(err)
		}
		ctx.Channels.Log <- mig.Log{OpID: opid, Desc: fmt.Sprintf("Investigator %.0f admin changed to %v", inv.ID, inv.IsAdmin)}
	}
	err = resource.AddItem(cljs.Item{
		Href: fmt.Sprintf("%s/investigator?investigatorid=%.0f", ctx.Server.BaseURL, inv.ID),
		Data: []cljs.Data{{Name: "Investigator ID " + fmt.Sprintf("%.0f", inv.ID), Value: inv}},
	})
	respond(http.StatusOK, resource, respWriter, request)
}