Commits

Enmanuel Rivera committed 14dda5a

Changed logistics as to how I work with empty dataset from databases.

Now returning a NOT_FOUND error from the database layer, instead of a nil model or empty interface. This is necessary because of how go deals with interfaces.

Comments (0)

Files changed (6)

resources/godfather-queries.json

 		 FROM users
 		 ORDER BY first_name, last_name",
 
+	"gf.get-users-by-state" :
+		"SELECT id_user, username, null password, first_name, last_name, enabled
+		 FROM users
+		 WHERE enabled = ?
+		 ORDER BY first_name, last_name",
+
 	"gf.get-users-in-group" :
 		"SELECT u.id_user, username, null password, first_name, last_name, enabled
 		 FROM users u, users_groups ug

src/gxs3/godfather/data/BaseDbDao.go

 	*/
 	CONSTRAINT error = errors.New("Constraint Violation")
 
+	/*
+	 Error that is raised when a logic data conflict arises. This is not the result of a constraint violation, but of a
+	 a potential logical inconsistency in the data
+	*/
+	CONFLICT error = errors.New("Data Conflict")
+
 	// Error that is raised when a query is executed, and no results are found.
 	NOT_FOUND error = errors.New("No data found")
 )

src/gxs3/godfather/data/UserDao.go

 }
 
 /*
+ Retrieves all users by enabled state
+*/
+func (ud *UserDao) GetByState(state bool) ([]*model.User, error) {
+	var stateInt uint
+
+	if state {
+		stateInt = 1
+	}
+
+	var query = ud.GetQuery("gf.get-users-by-state")
+	var result, err = ud.Template().Select(query, mapUser, stateInt)
+	return ud.cast(result), err
+}
+
+/*
  Retrieves all users which are members of the given group
 */
 func (ud *UserDao) GetInGroup(idGroup uint) ([]*model.User, error) {

src/gxs3/godfather/server/AuthService.go

 	user, err := as.userDao.GetByName(username)
 
 	if err != nil {
-		http.Error(writer, "Error attempting to retrieve user information: "+err.Error(), INTERNAL_SERVER_ERROR)
-		return
-	}
-
-	if user == nil {
-		http.Error(writer, "No user exists with that user name", NOT_FOUND)
+		if err == data.NOT_FOUND {
+			http.Error(writer, "No user exists with that user name", NOT_FOUND)
+		} else {
+			http.Error(writer, "Error attempting to retrieve user information: "+err.Error(), INTERNAL_SERVER_ERROR)
+		}
 		return
 	}
 

src/gxs3/godfather/server/ModelService.go

 
 	if err != nil {
 		if err == data.CONSTRAINT {
-			http.Error(writer, "Could not add model due to a data store constraint: "+err.Error(), CONFLICT)
+			http.Error(writer, "Could not add model due to a data store constraint violation: "+err.Error(), CONFLICT)
+		} else if err == data.CONFLICT {
+			http.Error(writer, "Could not execute the transaction due to the logical conflict: "+err.Error(), FORBIDDEN)
 		} else {
 			http.Error(writer, "Error adding model: "+err.Error(), INTERNAL_SERVER_ERROR)
 		}
 
 	if err != nil {
 		if err == data.CONSTRAINT {
-			http.Error(writer, "Could not update model due to a data store conflict: "+err.Error(), CONFLICT)
+			http.Error(writer, "Could not update model due to a data store constraint violation: "+err.Error(), CONFLICT)
+		} else if err == data.CONFLICT {
+			http.Error(writer, "Could not execute the transaction due to the logical conflict: "+err.Error(), FORBIDDEN)
 		} else {
 			http.Error(writer, "Error updating model: "+err.Error(), INTERNAL_SERVER_ERROR)
 		}
 
 	if err != nil {
 		if err == data.CONSTRAINT {
-			http.Error(writer, "Could not remove the model due to a data store conflict: "+err.Error(), CONFLICT)
+			http.Error(writer, "Could not remove the model due to a data store constraint violation: "+err.Error(), CONFLICT)
+		} else if err == data.CONFLICT {
+			http.Error(writer, "Could not execute the transaction due to the logical conflict: "+err.Error(), FORBIDDEN)
 		} else {
 			http.Error(writer, "Error removing the model: "+err.Error(), INTERNAL_SERVER_ERROR)
 		}
 
 	if err != nil {
 		if err == data.CONSTRAINT {
-			http.Error(writer, "Could not execute the transaction due to a data store conflict: "+err.Error(), CONFLICT)
+			http.Error(writer, "Could not execute the transaction due to a data store constraint violation: "+err.Error(), CONFLICT)
+		} else if err == data.CONFLICT {
+			http.Error(writer, "CoulD not execute the transaction due to the logical conflict: "+err.Error(), FORBIDDEN)
 		} else {
 			http.Error(writer, "Error executing the transaction: "+err.Error(), INTERNAL_SERVER_ERROR)
 		}

src/gxs3/godfather/server/UserService.go

 	PRIV_USER_REMOVE = "gf.user-remove" // Privilege to remove users
 
 	REL_USER_COLLECTION    = "/rels/gf.user-collection"    // Rel for link that retrieves the user collection
+	REL_USER_BY_STATE      = "/rels/gf.user-by-state"      // Rel for link that retrieves users by enabled state
 	REL_USER_ADD           = "/rels/gf.user-add"           // Rel for link that adds a user
 	REL_USER_GROUPS        = "/rels/gf.user-groups"        // Rel for link that retrieves users that belong to a group
 	REL_USER_GRANTED_PRIVS = "/rels/gf.user-granted-privs" // Rel fro link that retrieves privileges that have been granted to a user through his groups
 	REL_USER_REMOVE        = "/rels/gf.user-remove"        // Rel that removes a user
 
 	NAME     = "name"     // Used to extract name values from request queries
+	STATE    = "state"    // Used to extract state values from request queries
 	GROUP    = "group"    // Used to extract group values from request queries
 	NOTGROUP = "notgroup" // Used to extract "not group" values from request queries
 	PASSWORD = "password" // Used to extract password values from request queries
 
 	subrouter.Methods(GET).Path(ID_PATTERN).HandlerFunc(us.get)
 	subrouter.Methods(GET).Path("/{name:[a-zA-Z][a-zA-Z0-9_]*}").HandlerFunc(us.getByName)
+	subrouter.Methods(GET).Queries(STATE, "").HandlerFunc(us.getByState)
 	subrouter.Methods(GET).Queries(GROUP, "").HandlerFunc(us.getInGroup)
 	subrouter.Methods(GET).Queries(NOTGROUP, "").HandlerFunc(us.getNotInGroup)
 	subrouter.Methods(GET).MatcherFunc(ExactMatch(USER_COLLECTION)).HandlerFunc(us.getAll)
 	}
 
 	RegisterLink(&Link{JSON, REL_USER_COLLECTION, GET, USER_COLLECTION})
+	RegisterLink(&Link{JSON, REL_USER_BY_STATE, GET, USER_COLLECTION + "?state={state}"})
 	RegisterLink(&Link{PLAIN, REL_USER_ADD, GET, USER_COLLECTION})
 }
 
 }
 
 /*
+ Retrieves users by their enabled state
+ Method: GET - URI: /user?state={state}
+*/
+func (us *UserService) getByState(writer http.ResponseWriter, req *http.Request) {
+	var stateStr = req.FormValue(STATE)
+	var state, err = strconv.ParseBool(stateStr)
+
+	if err != nil {
+		http.Error(writer, "Error parsing the state argument: "+err.Error(), BAD_REQUEST)
+		return
+	}
+
+	var getByState = func() ([]model.Model, error) {
+		var users, err = us.dao.GetByState(state)
+		return us.toModels(users), err
+	}
+
+	us.GetCollection(writer, req, PRIV_USER_VIEW, getByState, us.Base().EncodeModels(encodeUser))
+}
+
+/*
  Retrieves all users that are members of a given group
  Method: GET - URI: /user?group={id-group}
 */