Merge branch 'develop' into listing
This commit is contained in:
commit
93f9979259
5 changed files with 35 additions and 16 deletions
|
|
@ -22,7 +22,8 @@
|
||||||
"owners": {
|
"owners": {
|
||||||
"type": "relation",
|
"type": "relation",
|
||||||
"relation": "manyToMany",
|
"relation": "manyToMany",
|
||||||
"target": "plugin::users-permissions.user"
|
"target": "plugin::users-permissions.user",
|
||||||
|
"mappedBy": "notes"
|
||||||
},
|
},
|
||||||
"lastViewed": {
|
"lastViewed": {
|
||||||
"type": "datetime",
|
"type": "datetime",
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
'use strict';
|
'use strict';
|
||||||
//move to utils!
|
//move to utils!
|
||||||
|
|
||||||
function getNoteIdFromUrl(url) {
|
function getNoteIdFromUrl(url) {
|
||||||
return Number(url.split("/").at(-1));
|
return Number(url.split("/").at(-1));
|
||||||
}
|
}
|
||||||
|
|
@ -43,7 +44,6 @@ module.exports = createCoreController(noteUid, ({strapi}) => ({
|
||||||
populate: ['owners'],
|
populate: ['owners'],
|
||||||
});
|
});
|
||||||
const authorized = entry.owners.some(owner => owner.id === userId)
|
const authorized = entry.owners.some(owner => owner.id === userId)
|
||||||
console.log(authorized)
|
|
||||||
if (authorized) {
|
if (authorized) {
|
||||||
entry = await strapi.entityService.update(noteUid, noteId, {
|
entry = await strapi.entityService.update(noteUid, noteId, {
|
||||||
data: {
|
data: {
|
||||||
|
|
@ -68,18 +68,37 @@ module.exports = createCoreController(noteUid, ({strapi}) => ({
|
||||||
populate: ['owners'],
|
populate: ['owners'],
|
||||||
});
|
});
|
||||||
const authorized = entry.owners.some(owner => owner.id === userId)
|
const authorized = entry.owners.some(owner => owner.id === userId)
|
||||||
let allowed;
|
let allPreviousOwnersKept = false;
|
||||||
if (requestBody.data.hasOwnProperty("owners")) {
|
if (requestBody.data.hasOwnProperty("owners")) {
|
||||||
allowed = entry.owners.every(owner => requestBody.data.owners.includes(owner));
|
allPreviousOwnersKept = entry.owners.every(owner => requestBody.data.owners.includes(owner));
|
||||||
}
|
}
|
||||||
if (!authorized) {
|
if (!authorized) {
|
||||||
ctx.response.status = 403;
|
ctx.response.status = 403;
|
||||||
} else if (!allowed) {
|
} else if (!allPreviousOwnersKept) {
|
||||||
ctx.response.status = 400;
|
ctx.response.status = 400;
|
||||||
} else {
|
} else {
|
||||||
return super.update(ctx);
|
return super.update(ctx);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
/**
|
||||||
|
* Creates a new note, automatically sets owners to the user making the request and lastViewed
|
||||||
|
* @param ctx
|
||||||
|
* @returns {Promise<ctx>}
|
||||||
|
*/
|
||||||
|
async create(ctx) {
|
||||||
|
const userId = ctx.state.user.id;
|
||||||
|
const requestBody = ctx.request.body;
|
||||||
|
const response = await strapi.entityService.create(noteUid, {
|
||||||
|
data: {
|
||||||
|
title: requestBody.data.title,
|
||||||
|
content: requestBody.data.content,
|
||||||
|
lastViewed: Date.now(),
|
||||||
|
owners: [userId],
|
||||||
|
publishedAt: Date.now()
|
||||||
|
}
|
||||||
|
});
|
||||||
|
return response;
|
||||||
|
},
|
||||||
/**
|
/**
|
||||||
* Deletes user from note owners. If note has no owners anymore, deletes note.
|
* Deletes user from note owners. If note has no owners anymore, deletes note.
|
||||||
* @param ctx
|
* @param ctx
|
||||||
|
|
|
||||||
|
|
@ -4,7 +4,7 @@ export interface NoteRepository {
|
||||||
getNotes(): Promise<Note[]>;
|
getNotes(): Promise<Note[]>;
|
||||||
getNote(id: number): Promise<Note>;
|
getNote(id: number): Promise<Note>;
|
||||||
getCurrentNote(): Promise<Note | void>;
|
getCurrentNote(): Promise<Note | void>;
|
||||||
updateNote(id: number, note: Note): Promise<Note>;
|
updateNote(id: number, note: Partial<Note>): Promise<Note>;
|
||||||
deleteNote(id: number): void;
|
deleteNote(id: number): void;
|
||||||
createNote(note: Note): Promise<Note>;
|
createNote(note: Partial<Note> & Pick<Note, 'title'>): Promise<Note>;
|
||||||
}
|
}
|
||||||
|
|
@ -19,12 +19,12 @@ export class StrapiNoteRepository implements NoteRepository {
|
||||||
private static apiNoteEndpoint: string = "http://localhost:1337/api/notes"
|
private static apiNoteEndpoint: string = "http://localhost:1337/api/notes"
|
||||||
|
|
||||||
public async getNotes(): Promise<Note[]>{
|
public async getNotes(): Promise<Note[]>{
|
||||||
const response = await StrapiNoteRepository.fetchStrapi("/", 'GET');
|
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/", 'GET');
|
||||||
return await response.json();
|
return await response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getNote(id: number): Promise<Note>{
|
public async getNote(id: number): Promise<Note>{
|
||||||
const response = await StrapiNoteRepository.fetchStrapi("/" + id, 'GET');
|
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/" + id, 'GET');
|
||||||
return await response.json();
|
return await response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -35,21 +35,21 @@ export class StrapiNoteRepository implements NoteRepository {
|
||||||
return await this.getNote(this.currentNoteId);
|
return await this.getNote(this.currentNoteId);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async updateNote(id: number, note: Note): Promise<Note> {
|
public async updateNote(id: number, note: Partial<Note>): Promise<Note> {
|
||||||
const response = await StrapiNoteRepository.fetchStrapi("/" + id, 'PUT', note);
|
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/" + id, 'PUT', note);
|
||||||
return await response.json();
|
return await response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async createNote(note: Note): Promise<Note> {
|
public async createNote(note: Partial<Note> & Pick<Note, 'title'>): Promise<Note> {
|
||||||
const response = await StrapiNoteRepository.fetchStrapi("/", 'POST', note);
|
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/", 'POST', note);
|
||||||
return await response.json();
|
return await response.json();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async deleteNote(id: number): Promise<void> {
|
public async deleteNote(id: number): Promise<void> {
|
||||||
await StrapiNoteRepository.fetchStrapi("/" + id, 'DELETE');
|
await StrapiNoteRepository.fetchStrapiNoteEndpoint("/" + id, 'DELETE');
|
||||||
}
|
}
|
||||||
|
|
||||||
private static async fetchStrapi(path: string, method: HttpMethod, body: Note | null = null): Promise<Response> {
|
private static async fetchStrapiNoteEndpoint(path: string, method: HttpMethod, body: Partial<Note> | null = null): Promise<Response> {
|
||||||
let requestInit: RequestInit = {
|
let requestInit: RequestInit = {
|
||||||
method: method,
|
method: method,
|
||||||
headers: {
|
headers: {
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,5 @@ export interface Note {
|
||||||
id: number;
|
id: number;
|
||||||
title: string;
|
title: string;
|
||||||
content: string;
|
content: string;
|
||||||
|
|
||||||
lastViewed: Date;
|
lastViewed: Date;
|
||||||
}
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue