コード例 #1
0
func SelectStateFromRecord(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	res.Header().Set("Access-Control-Allow-Origin", "*") // Allow for outside access.

	// Get Parameters
	sinfo, ok := GetStorageInfo(req)
	if !ok {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Missing Parameters, check documentation.",
			Code:   http.StatusNotAcceptable,
		}, sinfo)
		return
	}

	ctx := appengine.NewContext(req) // Make Context

	// Ensure user exists
	u := User{}
	getErr1 := retrievable.GetEntity(ctx, StorageKey{
		LoginDomain: sinfo.LoginDomain,
		ID:          sinfo.UUID,
	}, &u)
	if getErr1 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "No such user exists.",
			Code:   http.StatusNotFound,
		}, nil)
		return
	}

	// Get Record
	r := Record{}
	getErr2 := retrievable.GetFromDatastore(ctx, StorageKey{
		Domain: sinfo.Domain,
		ID:     sinfo.UUID,
	}, &r)
	if getErr2 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Success",
		}, struct {
			Status string
			User
		}{"User Not Logged In", u})
		return
	}

	ServeJsonOfStruct(res, JsonOptions{
		Status: "Success",
	}, struct {
		Status string
		User
	}{"User Logged In", u})
}
コード例 #2
0
func ToggleStateFromRecord(res http.ResponseWriter, req *http.Request, params httprouter.Params) {
	res.Header().Set("Access-Control-Allow-Origin", "*") // Allow for outside access.

	// Get Parameters
	sinfo, ok := GetStorageInfo(req)
	if !ok {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Missing Parameters, check documentation.",
			Code:   http.StatusNotAcceptable,
		}, sinfo)
		return
	}

	ctx := appengine.NewContext(req) // Make Context

	// Ensure user exists
	u := User{}
	getErr1 := retrievable.GetEntity(ctx, StorageKey{
		LoginDomain: sinfo.LoginDomain,
		ID:          sinfo.UUID,
	}, &u)
	if getErr1 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "No such user exists.",
			Code:   http.StatusNotFound,
		}, nil)
		return
	}

	// Get Record
	r := Record{}
	getErr2 := retrievable.GetFromDatastore(ctx, StorageKey{
		Domain: sinfo.Domain,
		ID:     sinfo.UUID,
	}, &r)
	if getErr2 != nil { // user is not logged in, lets do that.
		r = NewRecord(sinfo.UUID, sinfo.Domain)
		_, putErr1 := retrievable.PlaceInDatastore(ctx, StorageKey{
			Domain: sinfo.Domain,
			ID:     sinfo.UUID,
		}, &r)
		if putErr1 != nil {
			ServeJsonOfStruct(res, JsonOptions{
				Status: "Failure",
				Reason: "Internal Services Error (ts1)",
				Code:   http.StatusInternalServerError,
			}, nil)
			return
		}

		ServeJsonOfStruct(res, JsonOptions{
			Status: "Success",
		}, struct {
			Status string
			User
		}{
			"User is Logged In",
			u,
		})
		return
	}
	// User is logged in, lets move them to logout.
	r.Out = time.Now()

	delErr1 := retrievable.DeleteFromDatastore(ctx, StorageKey{
		Domain: sinfo.Domain,
		ID:     sinfo.UUID,
	}, &r)
	if delErr1 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Internal Services Error (ts2)",
			Code:   http.StatusInternalServerError,
		}, nil)
		return
	}

	_, putErr2 := retrievable.PlaceInDatastore(ctx, StorageKey{
		Domain: sinfo.Domain,
		ID:     fmt.Sprint(r.UUID, "-", r.In),
	}, &r)
	if putErr2 != nil {
		ServeJsonOfStruct(res, JsonOptions{
			Status: "Failure",
			Reason: "Internal Services Error (ts3)",
			Code:   http.StatusInternalServerError,
		}, nil)
		return
	}

	ServeJsonOfStruct(res, JsonOptions{
		Status: "Success",
	}, struct {
		Status string
		User
	}{
		"User is Logged out",
		u,
	})
}