Example #1
0
// Handles application routing
func GetRouter(env string, config *cfgloader.Config) *mux.Router {
	router := mux.NewRouter()
	router.HandleFunc("/{controller}/{id:[0-9]*}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		switch {
		case vars["controller"] == "country":
			controller.GetCountryController(env, config).GetCoreRouting(w, r)
		case vars["controller"] == "section":
			controller.GetSectionController(env, config).GetCoreRouting(w, r)
		case vars["controller"] == "competition":
			controller.GetCompetitionController(env, config).GetCoreRouting(w, r)
		case vars["controller"] == "top_competition":
			controller.GetTopCompetitionController(env, config).GetCoreRouting(w, r)
		default:
			controller.GetBaseController(env, config).WriteBadRequestResponse(w)
		}
	})

	// Parent - child relations
	router.HandleFunc("/{controller}/{entity_id:[0-9]+}/{child}/{child_id:[0-9a-z]*}", func(w http.ResponseWriter, r *http.Request) {
		vars := mux.Vars(r)
		switch {
		case vars["controller"] == "section":
			controller.GetSectionController(env, config).ManagementRouting(w, r)
		case vars["controller"] == "competition":
			controller.GetCompetitionController(env, config).ManagementRouting(w, r)
		case vars["controller"] == "top_competition":
			controller.GetTopCompetitionController(env, config).ManagementRouting(w, r)
		default:
			controller.GetBaseController(env, config).WriteBadRequestResponse(w)
		}
	})

	return router
}
Example #2
0
// Translate section
func (controller *SectionController) AddTranslation(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var (
		err    error
		exists bool
		data   map[string]string
	)

	vars := mux.Vars(r)

	if exists, err = model.GetSectionManager(dbMap).Exists(vars["entity_id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("ReadError"), err))

		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	if exists == false {
		return controller.GetErrorControllerResponse(http.StatusNotFound, GeneralServerErrorCode, controller.GetResponseMessage("NotFound"))
	}

	if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("BadRequest"))
	}

	if _, ok := data["name"]; !ok {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("BadRequest"))
	}

	if err = model.GetSectionManager(dbMap).AddTranslation(vars["entity_id"], vars["child_id"], data["name"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("AddTranslationError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("AddTranslationSuccess"), Empty{})
}
Example #3
0
// Routing
func (controller *CountryController) GetCoreRouting(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	switch {
	case r.Method == "GET" && vars["id"] == "":
		controller.ControllerMethodWrapper(controller.List, w, r)
	default:
		controller.WriteBadRequestResponse(w)
	}
}
Example #4
0
// Delete translation from top competition
func (controller *TopCompetitionController) RemoveCompetition(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var err error
	vars := mux.Vars(r)
	if err = model.GetTopCompetitionManager(dbMap).RemoveCompetition(vars["entity_id"], vars["child_id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("RemoveCompetitionError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("RemoveCompetitionSuccess"), Empty{})
}
Example #5
0
// Routing
func (controller *TopCompetitionController) ManagementRouting(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	switch {
	case r.Method == "POST" && vars["entity_id"] != "" && vars["child"] == "competition" && vars["child_id"] != "":
		controller.ControllerMethodWrapper(controller.AddCompetition, w, r)
	case r.Method == "DELETE" && vars["entity_id"] != "" && vars["child"] == "competition" && vars["child_id"] != "":
		controller.ControllerMethodWrapper(controller.RemoveCompetition, w, r)
	default:
		controller.WriteBadRequestResponse(w)
	}
}
Example #6
0
// Core routing
func (controller *TopCompetitionController) GetCoreRouting(w http.ResponseWriter, r *http.Request) {
	vars := mux.Vars(r)
	switch {
	case r.Method == "GET" && vars["id"] != "":
		controller.ControllerMethodWrapper(controller.Read, w, r)
	case r.Method == "DELETE" && vars["id"] != "":
		controller.ControllerMethodWrapper(controller.Delete, w, r)
	default:
		controller.WriteBadRequestResponse(w)
	}
}
Example #7
0
// Delete section
func (controller *SectionController) Delete(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var err error
	vars := mux.Vars(r)
	if err = model.GetSectionManager(dbMap).Delete(vars["id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details:records %s\n", controller.getLogPrefix(), controller.GetResponseMessage("DeleteError"), err))

		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("DeleteSuccess"), Empty{})
}
Example #8
0
// Read section
func (controller *SectionController) Read(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var (
		entity *model.Section
		err    error
	)
	vars := mux.Vars(r)
	if entity, err = model.GetSectionManager(dbMap).Read(vars["id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("ReadError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}
	if entity == nil {
		return controller.GetErrorControllerResponse(http.StatusNotFound, GeneralServerErrorCode, controller.GetResponseMessage("NotFound"))
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("ReadSuccess"), controller.GetResponseData(entity))
}
Example #9
0
// Update section
func (controller *SectionController) Update(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {

	var (
		data   map[string]interface{}
		exists bool
		entity *model.Section
		err    error
	)

	if err = json.NewDecoder(r.Body).Decode(&data); err != nil {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("BadRequest"))
	}

	if isValid := controller.ValidateData(data); !isValid {
		return controller.GetErrorControllerResponse(http.StatusBadRequest, GeneralFormValidationErrorCode, controller.GetResponseMessage("BadRequest"))
	}

	vars := mux.Vars(r)

	if exists, err = model.GetSectionManager(dbMap).Exists(vars["id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("ReadError"), err))

		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	if exists == false {
		return controller.GetErrorControllerResponse(http.StatusNotFound, GeneralServerErrorCode, controller.GetResponseMessage("NotFound"))
	}

	if err = model.GetSectionManager(dbMap).UpdateRecord(data); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("UpdateError"), err))

		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	if entity, err = model.GetSectionManager(dbMap).Read(vars["id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.GetResponseMessage("ReadError"), err))

		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("UpdateSuccess"), controller.GetResponseData(entity))
}