<commit_before>package node

import (
	"fmt"
	"net/http"
	"time"

	"github.com/MG-RAST/Shock/shock-server/conf"
	e "github.com/MG-RAST/Shock/shock-server/errors"
	"github.com/MG-RAST/Shock/shock-server/logger"
	"github.com/MG-RAST/Shock/shock-server/request"
	"github.com/MG-RAST/Shock/shock-server/responder"
	"github.com/MG-RAST/Shock/shock-server/user"
	"github.com/MG-RAST/golib/stretchr/goweb/context"
	"github.com/davecgh/go-spew/spew"
	mgo "gopkg.in/mgo.v2"
)

// GET, PUT, DELETE: /node/{nid}/locations/{loc}  , for PUT send body: {loc}, specify -H "Content-Type: application/json"
func LocationsRequest(ctx context.Context) {
	nid := ctx.PathValue("nid")
	locationID := ctx.PathValue("loc")

	rmeth := ctx.HttpRequest().Method

	u, err := request.Authenticate(ctx.HttpRequest())
	if err != nil && err.Error() != e.NoAuth {
		request.AuthError(err, ctx)
		return
	}

	// public user (no auth) can be used in some cases
	if u == nil {
		if (rmeth == "GET" && conf.ANON_READ) || (rmeth == "POST" && conf.ANON_WRITE) || (rmeth == "DELETE" && conf.ANON_WRITE) {
			u = &user.User{Uuid: "public"}
		} else {
			responder.RespondWithError(ctx, http.StatusUnauthorized, e.NoAuth)
			return
		}
	}

	// Load node by id
	n, err := Load(nid)
	if err != nil {
		if err == mgo.ErrNotFound {
			logger.Error("(LocationsRequest) (node.Load) id=" + nid + ": " + e.NodeNotFound)
			responder.RespondWithError(ctx, http.StatusNotFound, e.NodeNotFound)
		} else {
			// In theory the db connection could be lost between
			// checking user and load but seems unlikely.
			errMsg := "(LocationsRequest) (node.Load) id=" + nid + ":" + err.Error()
			logger.Error(errMsg)
			responder.RespondWithError(ctx, http.StatusInternalServerError, errMsg)
		}
		return
	}

	rights := n.Acl.Check(u.Uuid)
	if n.Acl.Owner != u.Uuid && u.Admin == false && n.Acl.Owner != "public" && rights["read"] == false {
		logger.Error("err@node_Acl: (Authenticate) id=" + nid + ": " + e.UnAuth)
		responder.RespondWithError(ctx, http.StatusUnauthorized, e.UnAuth)
		return
	}

	switch rmeth {

	//case "PUT": //PUT is idempotent
	case "GET":
		if locationID == "" { // /node/{nid}/locations
			locations := n.GetLocations()
			responder.RespondWithData(ctx, locations)
			return
		} else { // /node/{nid}/locations/{loc}
			var thisLocation Location
			thisLocation, err = n.GetLocation(locationID)
			if err != nil {
				responder.RespondWithError(ctx, http.StatusInternalServerError, err.Error())
				return
			}
			responder.RespondWithData(ctx, thisLocation)
			return

		}
	case "POST": // append
		if !u.Admin {
			errmsg := e.UnAuth
			if conf.DEBUG_AUTH {
				errmsg = "admin required"
			}

			responder.RespondWithError(ctx, http.StatusUnauthorized, errmsg) //
			return
		}

		var locationObjectIf interface{}
		locationObjectIf, err = ctx.RequestData()
		if err != nil {
			responder.RespondWithError(ctx, http.StatusInternalServerError, err.Error())
			return
		}
		spew.Dump(locationObjectIf)

		newLocation := Location{}<commit_msg>removed dump from POST method<commit_after>package node

import (
	"fmt"
	"net/http"
	"time"

	"github.com/MG-RAST/Shock/shock-server/conf"
	e "github.com/MG-RAST/Shock/shock-server/errors"
	"github.com/MG-RAST/Shock/shock-server/logger"
	"github.com/MG-RAST/Shock/shock-server/request"
	"github.com/MG-RAST/Shock/shock-server/responder"
	"github.com/MG-RAST/Shock/shock-server/user"
	"github.com/MG-RAST/golib/stretchr/goweb/context"
	mgo "gopkg.in/mgo.v2"
)

// GET, PUT, DELETE: /node/{nid}/locations/{loc}  , for PUT send body: {loc}, specify -H "Content-Type: application/json"
func LocationsRequest(ctx context.Context) {
	nid := ctx.PathValue("nid")
	locationID := ctx.PathValue("loc")

	rmeth := ctx.HttpRequest().Method

	u, err := request.Authenticate(ctx.HttpRequest())
	if err != nil && err.Error() != e.NoAuth {
		request.AuthError(err, ctx)
		return
	}

	// public user (no auth) can be used in some cases
	if u == nil {
		if (rmeth == "GET" && conf.ANON_READ) || (rmeth == "POST" && conf.ANON_WRITE) || (rmeth == "DELETE" && conf.ANON_WRITE) {
			u = &user.User{Uuid: "public"}
		} else {
			responder.RespondWithError(ctx, http.StatusUnauthorized, e.NoAuth)
			return
		}
	}

	// Load node by id
	n, err := Load(nid)
	if err != nil {
		if err == mgo.ErrNotFound {
			logger.Error("(LocationsRequest) (node.Load) id=" + nid + ": " + e.NodeNotFound)
			responder.RespondWithError(ctx, http.StatusNotFound, e.NodeNotFound)
		} else {
			// In theory the db connection could be lost between
			// checking user and load but seems unlikely.
			errMsg := "(LocationsRequest) (node.Load) id=" + nid + ":" + err.Error()
			logger.Error(errMsg)
			responder.RespondWithError(ctx, http.StatusInternalServerError, errMsg)
		}
		return
	}

	rights := n.Acl.Check(u.Uuid)
	if n.Acl.Owner != u.Uuid && u.Admin == false && n.Acl.Owner != "public" && rights["read"] == false {
		logger.Error("err@node_Acl: (Authenticate) id=" + nid + ": " + e.UnAuth)
		responder.RespondWithError(ctx, http.StatusUnauthorized, e.UnAuth)
		return
	}

	switch rmeth {

	//case "PUT": //PUT is idempotent
	case "GET":
		if locationID == "" { // /node/{nid}/locations
			locations := n.GetLocations()
			responder.RespondWithData(ctx, locations)
			return
		} else { // /node/{nid}/locations/{loc}
			var thisLocation Location
			thisLocation, err = n.GetLocation(locationID)
			if err != nil {
				responder.RespondWithError(ctx, http.StatusInternalServerError, err.Error())
				return
			}
			responder.RespondWithData(ctx, thisLocation)
			return

		}
	case "POST": // append
		if !u.Admin {
			errmsg := e.UnAuth
			if conf.DEBUG_AUTH {
				errmsg = "admin required"
			}

			responder.RespondWithError(ctx, http.StatusUnauthorized, errmsg) //
			return
		}

		var locationObjectIf interface{}
		locationObjectIf, err = ctx.RequestData()
		if err != nil {
			responder.RespondWithError(ctx, http.StatusInternalServerError, err.Error())
			return
		}
		//spew.Dump(locationObjectIf)

		newLocation := Location{}
{'fraction_non_alphanumeric': 0.15670274771024145, 'fraction_numerical': 0.00033305578684429644, 'mean_word_length': 3.6271186440677967, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3492947', 'n_tokens_mistral': 2407, 'n_tokens_neox': 2172, 'n_words': 436}