Skip to content
Merged
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
22 changes: 19 additions & 3 deletions docs/api/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,14 @@ components:
type: integer
example: 3

QuestClaimRequest:
type: object
required: [post_id]
properties:
post_id:
type: integer
example: 3

CreatePostRequest:
type: object
required: [title, content, tag, visibility]
Expand Down Expand Up @@ -438,7 +446,7 @@ components:
properties:
userQuest:
$ref: '#/components/schemas/UserQuest'
willBalance:
will_balance_of_user:
type: integer
example: 130

Expand Down Expand Up @@ -472,10 +480,12 @@ components:
InventoryResponse:
type: object
properties:
userItems:
type: array
$ref: '#/components/schemas/UserItem'
items:
type: array
items:
$ref: '#/components/schemas/InventoryItem'
$ref: '#/components/schemas/ShopItem'

paths:
# 0. SYSTEM CHECK
Expand Down Expand Up @@ -782,6 +792,12 @@ paths:
post:
summary: Claim completed quest reward
tags: [Quest]
requestBody:
required: true
content:
application/json:
schema:
$ref: '#/components/schemas/QuestClaimRequest'
security:
- bearerAuth: []
parameters:
Expand Down
73 changes: 69 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 8 additions & 11 deletions src/controllers/postController.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ async function getAll(req, res){
const user_id = Number(req.user.user_id);
const posts = await postModel.getAll(user_id);
//if(!posts || posts.length === 0) return res.status(404).json("no Posts found");
res.status(200).json(posts);
res.status(200).json({"posts": posts});
} catch (err){
console.error(err);
res.status(err.statusCode || 500).json({error: err.message});
Expand All @@ -20,7 +20,7 @@ async function getById(req, res){
const post_id = Number(req.params.id);
const post = await postModel.findById(post_id, user_id);
//if(!post) return res.status(404).json({error: "Post not found"});
return res.status(200).json(post);
return res.status(200).json({ "post": post});
} catch (err) {
console.error(err);
res.status(err.statusCode || 500).json({error: err.message});
Expand All @@ -33,8 +33,8 @@ async function getById(req, res){
// req.body would be undefined.
async function create(req, res) {
try {
const { user_id, title, content, image_url, tag, visibility, word_count,
will_reward, created_at, updated_at } = req.body;
const user_id = req.user.user_id;
const {title, content, image_url, tag, visibility, will_reward} = req.body;
// Basic validation: required fields must be present.
// Without this, an INSERT with NULL would fail at the database level
// because of the NOT NULL constraints we defined in db.js.
Expand All @@ -43,25 +43,22 @@ async function create(req, res) {
!title ||
!content ||
!tag ||
!visibility ||
//word_count === undefined ||
// will_reward === undefined ||
created_at == null ||
updated_at == null
!visibility
) {
return res.status(400).json({ error: "Missing required fields" });
}
const countTheWords = (str) => {return str.trim() .split(/\s+/).length;}
const wordCount = countTheWords(content);

const created_at = new Date().toISOString().split("T")[0];
const updated_at = new Date().toISOString().split("T")[0];
const calculatedWillReward = Math.round(wordCount / 10);

// TRANSACTION shizzle implementieren !! -> implement postService.js !!
const newPost = await postModel.create({ user_id, title, content, image_url, tag, visibility, word_count: wordCount, will_reward: calculatedWillReward, created_at, updated_at });

userModel.increaseWillAfterPost(calculatedWillReward, user_id);

res.status(201).json(newPost);
res.status(201).json({"post" : newPost});
} catch (err) {
console.error(err);
res.status(500).json({ error: "Database error" });
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/shopController.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function getAll(req, res) {
if(!items) {
return res.status(404).json({error: "Shop Items not found"});
}
return res.status(200).json(items);
return res.status(200).json({"items": items});
}
catch(err){
res.status(500).json({ error: `Database error : ${err}`});
Expand Down
6 changes: 1 addition & 5 deletions src/models/userModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ async function initDb(db) {
return db;
}

function getDb() {
return usersDb;
}

async function findById(user_id) {
return await usersDb.get("SELECT * FROM users WHERE user_id = ?", [user_id]);
}
Expand Down Expand Up @@ -174,7 +170,7 @@ async function decreaseWillIfEnough(user_id, price) {

async function increaseWillAfterPost(calculatedWillReward, user_id){

const result = await getDb().run(
const result = await usersDb.run(
`UPDATE users SET will_balance = will_balance + ? WHERE user_id = ?`,
[calculatedWillReward, user_id]);
}
Expand Down
9 changes: 5 additions & 4 deletions src/services/eggService.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,18 @@ async function equip({ user_id, item_id }) {
// if there is an item which is already equipped, unequip
if (prior != null && prior != item_id) {
let priorInventory = await userItemModel.findByIds(user_id, prior);

let priorItem = await shopItemModel.findById(prior);
if (priorInventory) {
priorInventory.is_equipped = 0;
await userItemModel.update(priorInventory);
}
applyItemEffect(egg, priorItem, 0);
}
if (prior != item_id){
applyItemEffect(egg, await shopItemModel.findById(prior), 0);

if(prior != item_id){
applyItemEffect(egg, item, 1);
}

egg[`active_${itemType}_id`] = itemInventory.item_id;
itemInventory.is_equipped = 1;
// equip item
Expand All @@ -66,7 +68,6 @@ async function equip({ user_id, item_id }) {

}


async function unequip({ user_id, item_id }) {
let egg = await eggModel.findById(user_id);
if (!egg) {
Expand Down
Loading