Merge branch 'develop' into listing

This commit is contained in:
dhain 2022-10-10 20:31:02 +02:00
commit 93f9979259
5 changed files with 35 additions and 16 deletions

View file

@ -22,7 +22,8 @@
"owners": {
"type": "relation",
"relation": "manyToMany",
"target": "plugin::users-permissions.user"
"target": "plugin::users-permissions.user",
"mappedBy": "notes"
},
"lastViewed": {
"type": "datetime",

View file

@ -1,5 +1,6 @@
'use strict';
//move to utils!
function getNoteIdFromUrl(url) {
return Number(url.split("/").at(-1));
}
@ -43,7 +44,6 @@ module.exports = createCoreController(noteUid, ({strapi}) => ({
populate: ['owners'],
});
const authorized = entry.owners.some(owner => owner.id === userId)
console.log(authorized)
if (authorized) {
entry = await strapi.entityService.update(noteUid, noteId, {
data: {
@ -68,18 +68,37 @@ module.exports = createCoreController(noteUid, ({strapi}) => ({
populate: ['owners'],
});
const authorized = entry.owners.some(owner => owner.id === userId)
let allowed;
let allPreviousOwnersKept = false;
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) {
ctx.response.status = 403;
} else if (!allowed) {
} else if (!allPreviousOwnersKept) {
ctx.response.status = 400;
} else {
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.
* @param ctx

View file

@ -4,7 +4,7 @@ export interface NoteRepository {
getNotes(): Promise<Note[]>;
getNote(id: number): Promise<Note>;
getCurrentNote(): Promise<Note | void>;
updateNote(id: number, note: Note): Promise<Note>;
updateNote(id: number, note: Partial<Note>): Promise<Note>;
deleteNote(id: number): void;
createNote(note: Note): Promise<Note>;
createNote(note: Partial<Note> & Pick<Note, 'title'>): Promise<Note>;
}

View file

@ -19,12 +19,12 @@ export class StrapiNoteRepository implements NoteRepository {
private static apiNoteEndpoint: string = "http://localhost:1337/api/notes"
public async getNotes(): Promise<Note[]>{
const response = await StrapiNoteRepository.fetchStrapi("/", 'GET');
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/", 'GET');
return await response.json();
}
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();
}
@ -35,21 +35,21 @@ export class StrapiNoteRepository implements NoteRepository {
return await this.getNote(this.currentNoteId);
}
public async updateNote(id: number, note: Note): Promise<Note> {
const response = await StrapiNoteRepository.fetchStrapi("/" + id, 'PUT', note);
public async updateNote(id: number, note: Partial<Note>): Promise<Note> {
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/" + id, 'PUT', note);
return await response.json();
}
public async createNote(note: Note): Promise<Note> {
const response = await StrapiNoteRepository.fetchStrapi("/", 'POST', note);
public async createNote(note: Partial<Note> & Pick<Note, 'title'>): Promise<Note> {
const response = await StrapiNoteRepository.fetchStrapiNoteEndpoint("/", 'POST', note);
return await response.json();
}
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 = {
method: method,
headers: {

View file

@ -2,6 +2,5 @@ export interface Note {
id: number;
title: string;
content: string;
lastViewed: Date;
}