Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Don't Merge] Edit note and category api #46

Open
wants to merge 6 commits into
base: publishApiv2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
127 changes: 121 additions & 6 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,10 +260,10 @@ func HandleNoteApiRequest(
return
}

fmt.Println("number of published notes")
fmt.Println(len(publishedNotes))
fmt.Println("number of unpublished notes")
fmt.Println(len(myUnpublishedNotes))
// fmt.Println("number of published notes")
// fmt.Println(len(publishedNotes))
// fmt.Println("number of unpublished notes")
// fmt.Println(len(myUnpublishedNotes))

allNotes := myUnpublishedNotes

Expand Down Expand Up @@ -306,7 +306,7 @@ func HandleNoteApiRequest(

note := &models.Note{
AuthorId: models.UserId(userId),
Content: noteForm.Content,
Content: strings.TrimSpace(noteForm.Content),
CreationTime: time.Now().UTC(),
}

Expand All @@ -331,6 +331,52 @@ func HandleNoteApiRequest(

fmt.Fprint(responseWriter, string(noteString))

case http.MethodPut:
type NoteForm struct {
Id int64 `json:"id"`
Content string `json:"content"`
}

noteForm := new(NoteForm)
if err := json.NewDecoder(request.Body).Decode(noteForm); err != nil {
http.Error(responseWriter, err.Error(), http.StatusBadRequest)
return
}

if noteForm.Id < 1 {
http.Error(responseWriter, "Invalid Note Id", http.StatusBadRequest)
return
}

noteId := models.NoteId(noteForm.Id)
note, err := env.Db.GetNoteById(noteId)
if err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}

if note.AuthorId != userId {
http.Error(responseWriter, "You can only edit notes of which you are the author", http.StatusUnauthorized)
return
}

content := strings.TrimSpace(noteForm.Content)
if len(content) == 0 {
http.Error(responseWriter, "Note content cannot be empty or just whitespace", http.StatusBadRequest)
return
}

if content == note.Content {
http.Error(responseWriter, "Note content is the same as existing content", http.StatusBadRequest)
return
}

if err := env.Db.UpdateNoteContent(noteId, content); err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}

responseWriter.WriteHeader(http.StatusOK)
case http.MethodDelete:

id, err := strconv.ParseInt(request.URL.Query().Get("id"), 10, 64)
Expand Down Expand Up @@ -373,6 +419,31 @@ func HandleCategoryApiRequest(
userId models.UserId,
) {
switch request.Method {
case http.MethodGet:

id, err := strconv.ParseInt(request.URL.Query().Get("id"), 10, 64)
noteId := models.NoteId(id)

category, err := env.Db.GetNoteCategory(noteId)
if err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}

type categoryObj struct {
Category string `json:"category"`
}

jsonValue, err := json.Marshal(&categoryObj{Category: category.String()})
if err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}

responseWriter.Header().Set("Content-Type", "application/json")
responseWriter.WriteHeader(http.StatusOK)

fmt.Fprint(responseWriter, string(jsonValue))
case http.MethodPost:

type CategoryForm struct {
Expand Down Expand Up @@ -401,8 +472,52 @@ func HandleCategoryApiRequest(

responseWriter.WriteHeader(http.StatusCreated)

case http.MethodPut:
type CategoryForm struct {
NoteId int64 `json:"noteId"`
Category string `json:"category"`
}

noteForm := new(CategoryForm)

if err := json.NewDecoder(request.Body).Decode(noteForm); err != nil {
http.Error(responseWriter, err.Error(), http.StatusBadRequest)
return
}

category, err := models.DeserializeCategory(strings.ToLower(noteForm.Category))

if err != nil {
http.Error(responseWriter, err.Error(), http.StatusBadRequest)
return
}

if err := env.Db.UpdateNoteCategory(models.NoteId(noteForm.NoteId), category); err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}

responseWriter.WriteHeader(http.StatusOK)

case http.MethodDelete:

id, err := strconv.ParseInt(request.URL.Query().Get("id"), 10, 64)
if err != nil {
http.Error(responseWriter, err.Error(), http.StatusBadRequest)
return
}

noteId := models.NoteId(id)

if err := env.Db.DeleteNoteCategory(noteId); err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}

responseWriter.WriteHeader(http.StatusOK)

default:
respondWithMethodNotAllowed(responseWriter, http.MethodPost)
respondWithMethodNotAllowed(responseWriter, http.MethodPost, http.MethodPut, http.MethodDelete)
}

}
Expand Down
Loading