Browse Source

the start of card management

develop
Rob Colbert 2 years ago
parent
commit
e56a7e36af
  1. 1
      app/controllers/admin.js
  2. 112
      app/controllers/admin/card.js
  3. 2
      app/controllers/admin/deck.js
  4. 6
      app/models/card.js
  5. 99
      app/services/card.js
  6. 2
      app/services/deck.js
  7. 4
      app/views/admin/card/index.pug

1
app/controllers/admin.js

@ -44,6 +44,7 @@ class AdminController extends SiteController {
}),
);
router.use('/card',await this.loadChild(path.join(__dirname, 'admin', 'card')));
router.use('/content-report',await this.loadChild(path.join(__dirname, 'admin', 'content-report')));
router.use('/deck',await this.loadChild(path.join(__dirname, 'admin', 'deck')));
router.use('/host',await this.loadChild(path.join(__dirname, 'admin', 'host')));

112
app/controllers/admin/card.js

@ -0,0 +1,112 @@
// admin/card.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0
'use strict';
const DTP_COMPONENT_NAME = 'admin:card';
const express = require('express');
// const mongoose = require('mongoose');
const { SiteController, SiteError } = require('../../../lib/site-lib');
class CardController extends SiteController {
constructor (dtp) {
super(dtp, DTP_COMPONENT_NAME);
}
async start ( ) {
const router = express.Router();
router.use(async (req, res, next) => {
res.locals.currentView = 'admin';
res.locals.adminView = 'card';
return next();
});
router.param('cardId', this.populateCardId.bind(this));
router.post('/:cardId', this.postCardUpdate.bind(this));
router.post('/', this.postCardCreate.bind(this));
router.get('/create', this.getCardCreateForm.bind(this));
router.get('/:cardId', this.getCardView.bind(this));
router.get('/', this.getHomeView.bind(this));
return router;
}
async populateCardId (req, res, next, cardId) {
const { card: cardService } = this.dtp.services;
try {
res.locals.card = await cardService.getById(cardId);
if (!res.locals.card) {
throw new SiteError(404, 'Card not found');
}
return next();
} catch (error) {
this.log.error('failed to populate card', { cardId, error });
return next(error);
}
}
async postCardUpdate (req, res, next) {
const { card: cardService } = this.dtp.services;
try {
await cardService.update(res.locals.card, req.body);
res.redirect(`/admin/card/${res.locals.card._id}`);
} catch (error) {
this.log.error('failed to update card', {
cardId: res.locals.card._id,
error,
});
return next(error);
}
}
async postCardCreate (req, res, next) {
const { card: cardService } = this.dtp.services;
try {
res.locals.card = await cardService.create(req.user, req.body);
res.redirect(`/admin/card/${res.locals.card._id}`);
} catch (error) {
this.log.error('failed to create card', {
cardId: res.locals.card._id,
error,
});
return next(error);
}
}
async getCardView (req, res) {
res.render('admin/card/editor');
}
async getCardCreateForm (req, res) {
res.render('admin/card/editor');
}
async getHomeView (req, res, next) {
const { card: cardService } = this.dtp.services;
try {
res.locals.pagination = this.getPaginationParameters(req, 20);
res.locals.cards = await cardService.getCards(res.locals.pagination);
res.render('admin/card/index');
} catch (error) {
this.log.error('failed to create card', {
error,
});
return next(error);
}
}
}
module.exports = async (dtp) => {
let controller = new CardController(dtp);
return controller;
};

2
app/controllers/admin/deck.js

@ -58,7 +58,7 @@ class DeckController extends SiteController {
async postDeckUpdate (req, res, next) {
const { deck: deckService } = this.dtp.services;
try {
res.locals.deck = await deckService.update(res.locals.deck, req.body);
await deckService.update(res.locals.deck, req.body);
res.redirect(`/admin/deck/${res.locals.deck._id}`);
} catch (error) {
this.log.error('failed to update deck', {

6
app/models/card.js

@ -1,4 +1,4 @@
// playing-card.js
// card.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0
@ -22,7 +22,7 @@ const MediaMetadataSchema = new Schema({
},
});
const PlayingCardSchema = new Schema({
const CardSchema = new Schema({
deck: { type: Schema.ObjectId, required: true, index: 1 },
type: { type: String, enum: CARD_TYPE_LIST, required: true, index: 1 },
content: {
@ -42,4 +42,4 @@ const PlayingCardSchema = new Schema({
},
});
module.exports = mongoose.model('PlayingCard', PlayingCardSchema);
module.exports = mongoose.model('Card', CardSchema);

99
app/services/card.js

@ -0,0 +1,99 @@
// card.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0
'use strict';
const mongoose = require('mongoose');
// const Card = mongoose.model('Card');
const Card = mongoose.model('Card');
const striptags = require('striptags');
const { SiteService } = require('../../lib/site-lib');
class CardService extends SiteService {
constructor (dtp) {
super(dtp, module.exports);
this.populateCard = [
{
path: 'owner',
select: 'created username username_lc displayName picture',
},
];
}
async create (owner, cardDefinition) {
const NOW = new Date();
const card = new Card();
card.created = NOW;
card.owner = owner._id;
card.name = striptags(cardDefinition.name.trim());
card.name_lc = card.name.toLowerCase();
card.description = striptags(cardDefinition.description.trim());
if (cardDefinition.tags) {
card.tags = cardDefinition.tags.split(',').map((tag) => tag.trim());
}
card.flags = {
isOfficial: cardDefinition.isOfficial === 'on',
};
await card.save();
return card.toObject();
}
async update (card, cardDefinition) {
cardDefinition.name = striptags(cardDefinition.name.trim());
cardDefinition.name_lc = cardDefinition.name.toLowerCase();
cardDefinition.description = striptags(cardDefinition.description.trim());
if (cardDefinition.tags) {
cardDefinition.tags = cardDefinition.tags.split(',').map((tag) => tag.trim());
}
await Card.updateOne(
{ _id: card._id },
{
$set: {
name: cardDefinition.name,
name_lc: cardDefinition.name_lc,
description: cardDefinition.description,
tags: cardDefinition.tags,
'flags.isOfficial': cardDefinition.isOfficial === 'on',
},
},
);
}
async getCards (pagination) {
const cards = await Card
.find()
.sort({ created: -1 })
.skip(pagination.skip)
.limit(pagination.cpp)
.populate(this.populateCard)
.lean();
return cards;
}
async getById (cardId) {
const card = await Card
.findOne({ _id: cardId })
.populate(this.populateCard)
.lean();
return card;
}
}
module.exports = {
slug: 'card',
name: 'card',
create: (dtp) => { return new CardService(dtp); },
};

2
app/services/deck.js

@ -6,7 +6,7 @@
const mongoose = require('mongoose');
// const PlayingCard = mongoose.model('PlayingCard');
// const Card = mongoose.model('Card');
const Deck = mongoose.model('Deck');
const striptags = require('striptags');

4
app/views/admin/card/index.pug

@ -0,0 +1,4 @@
extends ../layouts/main
block content
h1 Stuff
Loading…
Cancel
Save