コード例 #1
0
ファイル: competition.go プロジェクト: vslovik/http-api
// Add translation
func (controller *CompetitionController) 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.GetCompetitionManager(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.GetCompetitionManager(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{})
}
コード例 #2
0
ファイル: competition.go プロジェクト: vslovik/http-api
// List competitions
func (controller *CompetitionController) List(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var (
		err             error
		competitionList *[]model.Competition
		total           int
		page            int
	)
	query := r.URL.Query()

	if list, ok := query["page"]; ok {
		page, err = strconv.Atoi(list[0])
		if page == 0 {
			page = 1
		}
	} else {
		page = 1
	}
	if err != nil {
		page = 1
	}

	if competitionList, total, err = model.GetCompetitionManager(dbMap).List(page); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details:records %s\n", controller.getLogPrefix(), controller.GetResponseMessage("ListError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("ListSuccess"), CompetitionControllerPaginatedList{Competitions: competitionList, Total: total})
}
コード例 #3
0
ファイル: competition.go プロジェクト: vslovik/http-api
// Hide competition
func (controller *CompetitionController) Hide(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var err error
	vars := mux.Vars(r)
	if err = model.GetCompetitionManager(dbMap).Hide(vars["id"]); err != nil {
		new(syslog.Writer).Err(fmt.Sprintf("%s %s Details: %s\n", controller.getLogPrefix(), controller.GetResponseMessage("HideError"), err))
		return controller.GetErrorControllerResponse(http.StatusInternalServerError, GeneralServerErrorCode, "DB: "+err.Error())
	}

	return controller.GetSuccessControllerResponse(http.StatusOK, controller.GetResponseMessage("HideSuccess"), Empty{})
}
コード例 #4
0
ファイル: competition.go プロジェクト: vslovik/http-api
// Read section
func (controller *CompetitionController) Read(dbMap *gorp.DbMap, r *http.Request) *ControllerResponse {
	var (
		entity *model.Competition
		err    error
	)
	vars := mux.Vars(r)
	if entity, err = model.GetCompetitionManager(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))
}