|
|
@ -124,7 +124,7 @@ func (s *Server) UserEndpoint(w http.ResponseWriter, req *http.Request) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) TodoEndpoint(w http.ResponseWriter, req *http.Request) { |
|
|
|
func (s *Server) SpecificTodoEndpoint(w http.ResponseWriter, req *http.Request) { |
|
|
|
switch req.Method { |
|
|
|
switch req.Method { |
|
|
|
case http.MethodDelete: |
|
|
|
case http.MethodDelete: |
|
|
|
// Delete an existing TODO
|
|
|
|
// Delete an existing TODO
|
|
|
@ -150,35 +150,92 @@ func (s *Server) TodoEndpoint(w http.ResponseWriter, req *http.Request) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Mark TODO as done and assign a completion time
|
|
|
|
// // Mark TODO as done and assign a completion time
|
|
|
|
updatedTodo, err := s.db.GetTodo(todoID) |
|
|
|
// updatedTodo, err := s.db.GetTodo(todoID)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// logger.Error("[Server] Failed to get todo with id %d for marking completion: %s", todoID, err)
|
|
|
|
|
|
|
|
// http.Error(w, "TODO retrieval error", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
// updatedTodo.IsDone = true
|
|
|
|
|
|
|
|
// updatedTodo.CompletionTimeUnix = uint64(time.Now().Unix())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// err = s.db.UpdateTodo(todoID, *updatedTodo)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// logger.Error("[Server] Failed to update TODO with id %d: %s", todoID, err)
|
|
|
|
|
|
|
|
// http.Error(w, "Failed to update TODO information", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Now delete
|
|
|
|
|
|
|
|
err = s.db.DeleteTodo(todoID) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
logger.Error("[Server] Failed to get todo with id %d for marking completion: %s", todoID, err) |
|
|
|
logger.Error("[Server] Failed to delete %s's TODO: %s", GetUsernameFromAuth(req), err) |
|
|
|
http.Error(w, "TODO retrieval error", http.StatusInternalServerError) |
|
|
|
http.Error(w, "Failed to delete TODO", http.StatusInternalServerError) |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
updatedTodo.IsDone = true |
|
|
|
|
|
|
|
updatedTodo.CompletionTimeUnix = uint64(time.Now().Unix()) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
err = s.db.UpdateTodo(todoID, *updatedTodo) |
|
|
|
// Success!
|
|
|
|
|
|
|
|
logger.Info("[Server] Deleted TODO with ID %d", todoID) |
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case http.MethodPost: |
|
|
|
|
|
|
|
// Change TODO information
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Check authentication information
|
|
|
|
|
|
|
|
if !IsRequestAuthValid(req, s.db) { |
|
|
|
|
|
|
|
http.Error(w, "Invalid user auth data", http.StatusForbidden) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Obtain TODO ID
|
|
|
|
|
|
|
|
todoIDStr := path.Base(req.URL.Path) |
|
|
|
|
|
|
|
todoID, err := strconv.ParseUint(todoIDStr, 10, 64) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
logger.Error("[Server] Failed to update TODO with id %d: %s", todoID, err) |
|
|
|
http.Error(w, "Invalid TODO ID", http.StatusBadRequest) |
|
|
|
http.Error(w, "Failed to update TODO information", http.StatusInternalServerError) |
|
|
|
|
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Now delete
|
|
|
|
// Check if the user owns this TODO
|
|
|
|
// err = s.db.DeleteTodo(todoID)
|
|
|
|
if !DoesUserOwnTodo(GetUsernameFromAuth(req), todoID, s.db) { |
|
|
|
// if err != nil {
|
|
|
|
http.Error(w, "You don't own this TODO", http.StatusForbidden) |
|
|
|
// logger.Error("[Server] Failed to delete %s's TODO: %s", GetUsernameFromAuth(req), err)
|
|
|
|
return |
|
|
|
// http.Error(w, "Failed to delete TODO", http.StatusInternalServerError)
|
|
|
|
} |
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Success!
|
|
|
|
// Read body
|
|
|
|
logger.Info("[Server] updated (marked as done) TODO with ID %d", todoID) |
|
|
|
body, err := io.ReadAll(req.Body) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
logger.Warning("[Server] Failed to read request body to possibly update a TODO: %s", err) |
|
|
|
|
|
|
|
http.Error(w, "Failed to read body", http.StatusInternalServerError) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Unmarshal JSON
|
|
|
|
|
|
|
|
var updatedTodo db.Todo |
|
|
|
|
|
|
|
err = json.Unmarshal(body, &updatedTodo) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
logger.Warning("[Server] Received invalid TODO JSON in order to update: %s", err) |
|
|
|
|
|
|
|
http.Error(w, "Invalid TODO JSON", http.StatusBadRequest) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Update. (Creation date, owner username and an ID do not change)
|
|
|
|
|
|
|
|
err = s.db.UpdateTodo(todoID, updatedTodo) |
|
|
|
|
|
|
|
if err != nil { |
|
|
|
|
|
|
|
logger.Warning("[Server] Failed to update TODO: %s", err) |
|
|
|
|
|
|
|
http.Error(w, "Failed to update", http.StatusBadRequest) |
|
|
|
|
|
|
|
return |
|
|
|
|
|
|
|
} |
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
|
|
|
|
logger.Info("[Server] Updated TODO with ID %d", todoID) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
default: |
|
|
|
|
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (s *Server) TodoEndpoint(w http.ResponseWriter, req *http.Request) { |
|
|
|
|
|
|
|
switch req.Method { |
|
|
|
case http.MethodPost: |
|
|
|
case http.MethodPost: |
|
|
|
// Create a new TODO
|
|
|
|
// Create a new TODO
|
|
|
|
defer req.Body.Close() |
|
|
|
defer req.Body.Close() |
|
|
@ -218,6 +275,7 @@ func (s *Server) TodoEndpoint(w http.ResponseWriter, req *http.Request) { |
|
|
|
// Success!
|
|
|
|
// Success!
|
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
w.WriteHeader(http.StatusOK) |
|
|
|
logger.Info("[Server] Created a new TODO for %s", newTodo.OwnerUsername) |
|
|
|
logger.Info("[Server] Created a new TODO for %s", newTodo.OwnerUsername) |
|
|
|
|
|
|
|
|
|
|
|
case http.MethodGet: |
|
|
|
case http.MethodGet: |
|
|
|
// Retrieve TODO information
|
|
|
|
// Retrieve TODO information
|
|
|
|
// Check authentication information
|
|
|
|
// Check authentication information
|
|
|
@ -226,7 +284,7 @@ func (s *Server) TodoEndpoint(w http.ResponseWriter, req *http.Request) { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Get TODO
|
|
|
|
// Get all user TODOs
|
|
|
|
todos, err := s.db.GetAllUserTodos(GetUsernameFromAuth(req)) |
|
|
|
todos, err := s.db.GetAllUserTodos(GetUsernameFromAuth(req)) |
|
|
|
if err != nil { |
|
|
|
if err != nil { |
|
|
|
http.Error(w, "Failed to get TODOs", http.StatusInternalServerError) |
|
|
|
http.Error(w, "Failed to get TODOs", http.StatusInternalServerError) |
|
|
@ -243,42 +301,6 @@ func (s *Server) TodoEndpoint(w http.ResponseWriter, req *http.Request) { |
|
|
|
// Send out
|
|
|
|
// Send out
|
|
|
|
w.Header().Add("Content-Type", "application/json") |
|
|
|
w.Header().Add("Content-Type", "application/json") |
|
|
|
w.Write(todosBytes) |
|
|
|
w.Write(todosBytes) |
|
|
|
|
|
|
|
|
|
|
|
// case http.MethodPatch:
|
|
|
|
|
|
|
|
// // Change TODO due date and text
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Check authentication information
|
|
|
|
|
|
|
|
// if !IsRequestAuthValid(req, s.db) {
|
|
|
|
|
|
|
|
// http.Error(w, "Invalid user auth data", http.StatusForbidden)
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Read body
|
|
|
|
|
|
|
|
// body, err := io.ReadAll(req.Body)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// logger.Warning("[Server] Failed to read request body to possibly update a TODO: %s", err)
|
|
|
|
|
|
|
|
// http.Error(w, "Failed to read body", http.StatusInternalServerError)
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // Unmarshal JSON
|
|
|
|
|
|
|
|
// var todo db.Todo
|
|
|
|
|
|
|
|
// err = json.Unmarshal(body, &todo)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// logger.Warning("[Server] Received invalid TODO JSON in order to update: %s", err)
|
|
|
|
|
|
|
|
// http.Error(w, "Invalid TODO JSON", http.StatusBadRequest)
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// // TODO
|
|
|
|
|
|
|
|
// err = s.db.UpdateTodo(todo.ID, todo)
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
|
|
|
|
// logger.Warning("[Server] Failed to update TODO: %s", err)
|
|
|
|
|
|
|
|
// http.Error(w, "Failed to update", http.StatusBadRequest)
|
|
|
|
|
|
|
|
// return
|
|
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// w.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
default: |
|
|
|
default: |
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
|
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) |
|
|
|
} |
|
|
|
} |
|
|
|