func Get(configObject config.ContextObject) []byte { rows, err := configObject.Db.Query(dbSelectQuery) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) } dbData := converters.ConvertRowsToStructObjects(rows) data, err := json.Marshal(dbData) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) } return data }
func main() { configObject := config.ContextObject{} errorLogFilePath := "errorLog" errorFile, err := os.OpenFile(errorLogFilePath, os.O_APPEND|os.O_WRONLY, 0600) if err != nil { panic(err) } defer errorFile.Close() configObject.ErrorLogFile = errorFile dbConfigFilePath := "dbConfigFile" if len(os.Args) > 1 { dbConfigFilePath = os.Args[1] } dbConfigDataJson := fileReaders.ReadJsonFile(dbConfigFilePath) dbinfo := database.CreateDbInfo(dbConfigDataJson) configObject.Db, err = sql.Open("postgres", dbinfo) configObject.Db.Ping() if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) } defer configObject.Db.Close() routers.HandleRequests(configObject) err = http.ListenAndServe(":9090", nil) if err != nil { fmt.Println("their was error ", err) } }
func (task *Task) Create(configObject config.ContextObject) error { _, err := configObject.Db.Exec(dbInsertQuery, task.taskDescription, task.priority) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) return err } return nil }
func AddTaskByCsv(configObject config.ContextObject, data string) error { separatedData, err := fileReaders.ReadTaskCsv(data) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) return err } for _, each := range separatedData { err := Add(configObject, each.TASK, each.PRIORITY) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) return err } } return nil }
func UpdateTaskDescription(configObject config.ContextObject, taskId int, data string) error { _, err := configObject.Db.Exec(dbDescriptionUpdateQuery, data, taskId) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) return err } return nil }
func UpdatePriority(configObject config.ContextObject, taskId int, priority string) error { _, err := configObject.Db.Exec(dbPriorityUpdateQuery, priority, taskId) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) return err } return nil }
func DeleteTask(configObject config.ContextObject) http.HandlerFunc { return func(res http.ResponseWriter, req *http.Request) { req.ParseForm() taskId := strings.Split(req.RequestURI, "/")[2] task, err := strconv.Atoi(taskId) err = models.Delete(configObject, task) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) res.WriteHeader(http.StatusInternalServerError) } res.WriteHeader(http.StatusAccepted) } }
func AddTask(configObject config.ContextObject) http.HandlerFunc { return func(res http.ResponseWriter, req *http.Request) { req.ParseForm() taskDescription := strings.Join(req.Form["task"], "") priority := strings.Join(req.Form["priority"], "") err := models.Add(configObject, taskDescription, priority) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) res.WriteHeader(http.StatusInternalServerError) } res.WriteHeader(http.StatusCreated) } }
func UpdateTaskDescription(configObject config.ContextObject) http.HandlerFunc { return func(res http.ResponseWriter, req *http.Request) { req.ParseForm() taskId := strings.Join(req.Form["taskId"], "") data := strings.Join(req.Form["data"], "") id, _ := strconv.Atoi(taskId) err := models.UpdateTaskDescription(configObject, id, data) if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) res.WriteHeader(http.StatusInternalServerError) } res.WriteHeader(http.StatusCreated) } }
func UploadCsv(configObject config.ContextObject) http.HandlerFunc { return func(res http.ResponseWriter, req *http.Request) { file, _, err := req.FormFile("uploadFile") if err != nil { errorHandler.ErrorHandler(configObject.ErrorLogFile, err) } defer file.Close() data, err := ioutil.ReadAll(file) err = models.AddTaskByCsv(configObject, string(data)) if err != nil { res.WriteHeader(http.StatusInternalServerError) } res.WriteHeader(http.StatusOK) } }