Skip to content
Closed
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
70 changes: 70 additions & 0 deletions databaseutil/databaseutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,76 @@ func GetPasswordForUserWithEmailAddress(emailAddress string) ([]byte, error) {
return password, nil
}

type NoteData struct {
Id int64
AuthorId int64
Content string
CreationTime time.Time
}

func returnNotes(rows *sql.Rows) ([]*NoteData, error) {
var notes []*NoteData = make([]*NoteData, 0, 10)

for rows.Next() {
note := &NoteData{}
if err := rows.Scan(&note.Id, &note.AuthorId, &note.Content, &note.CreationTime); err != nil {
return nil, err
}

notes = append(notes, note)
}

return notes, nil
}

func GetNote(id int64) (*NoteData, error) {

sqlQuery := `
SELECT id, author_id, content, creation_time FROM note
WHERE id = $1`

rows, err := db.Query(sqlQuery, id)
if err != nil {
return nil, convertPostgresError(err)
}

defer rows.Close()

notes, err := returnNotes(rows)
if err != nil {
return nil, err
}

if len(notes) > 1 {

return nil, QueryResultContainedMultipleRowsError
}

if len(notes) < 1 {
return nil, QueryResultContainedNoRowsError
}

return notes[0], nil
}

func DeleteNote(id int64) error {
sqlQuery := `
DELETE FROM note
WHERE id = $1`

rows, err := db.Query(sqlQuery, id)
if err != nil {
return convertPostgresError(err)
}
defer rows.Close()

if err := rows.Err(); err != nil {
return convertPostgresError(err)
}

return nil
}

func StoreNewNote(authorId int64, content string, creationTime time.Time) (int64, error) {
sqlQuery := `
INSERT INTO note (author_id, content, creation_time)
Expand Down
29 changes: 28 additions & 1 deletion handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"html/template"
"net/http"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -292,8 +293,34 @@ func HandleNoteApiRequest(

responseWriter.WriteHeader(http.StatusCreated)

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
}

note, err := noteservice.GetNoteById(id)
if err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}

if note.AuthorId != userId {
errorString := "You are not the author of this note. you are only allowed to delete your own notes"
http.Error(responseWriter, errorString, http.StatusUnauthorized)
return
}

err = noteservice.DeleteNoteById(note.Id)
if err != nil {
http.Error(responseWriter, err.Error(), http.StatusInternalServerError)
return
}

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

Expand Down
8 changes: 4 additions & 4 deletions migrations/0000_createDbs.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,17 @@ CREATE TABLE IF NOT EXISTS publication (

CREATE TABLE IF NOT EXISTS note (
id bigserial PRIMARY KEY,
author_id bigint references app_user(id) NOT NULL,
author_id bigint references app_user(id) ON DELETE CASCADE NOT NULL,
content text NOT NULL,
creation_time timestamp NOT NULL
);

CREATE TABLE IF NOT EXISTS note_to_publication_relationship (
note_id bigint PRIMARY KEY references note(id),
publication_id bigint references publication(id) NOT NULL
note_id bigint PRIMARY KEY references note(id) ON DELETE CASCADE,
publication_id bigint references publication(id) ON DELETE CASCADE NOT NULL
);

CREATE TABLE IF NOT EXISTS note_to_category_relationship (
note_id bigint PRIMARY KEY references note(id),
note_id bigint PRIMARY KEY references note(id) ON DELETE CASCADE,
type category NOT NULL
);
2 changes: 1 addition & 1 deletion migrations/tools/drop_everything.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ DROP TABLE publication CASCADE;

DROP TABLE note CASCADE;

DROP TABLE note_to_type_relationship CASCADE;
DROP TABLE note_to_category_relationship CASCADE;

DROP TABLE note_to_publication_relationship CASCADE;
20 changes: 20 additions & 0 deletions services/noteservice/noteservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,26 @@ func StoreNewNote(
return nil
}

func GetNoteById(id int64) (*models.Note, error) {
noteData, err := databaseutil.GetNote(id)

if err != nil {
return nil, err
}

return &models.Note{
Id: noteData.Id,
AuthorId: models.UserId(noteData.AuthorId),
Content: noteData.Content,
CreationTime: noteData.CreationTime,
},
nil
}

func DeleteNoteById(id int64) error {
return databaseutil.DeleteNote(id)
}

func StoreNoteCategoryRelationship(
note *models.Note,
category models.Category,
Expand Down
32 changes: 32 additions & 0 deletions static/js/base.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
$(function() {
// http://stepansuvorov.com/blog/2014/04/jquery-put-and-delete/
jQuery.each( [ "put", "delete" ], function( i, method ) {
jQuery[ method ] = function( url, data, callback, type ) {
if ( jQuery.isFunction( data ) ) {
type = type || callback;
callback = data;
data = undefined;
}

return jQuery.ajax({
url: url,
type: method,
dataType: type,
data: data,
success: callback
});
};
});

jQuery.prototype.getDOM = function() {
if (this.length === 1) {
return this[0];
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

if (this.length === 0) {
throw "jQuery object is empty"
}
throw "jQuery Object contains more than 1 object";
};

});
1 change: 1 addition & 0 deletions templates/base.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<script src="//cdn.muicss.com/mui-0.9.36/js/mui.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/moment.js/2.22.2/moment.min.js"></script>
<script src="/static/js/base.js"></script>
{{ block "js" . }} {{ end }}
</head>
<body>
Expand Down