many fixes in design and add shirt story type for creation book
This commit is contained in:
+26
-14
@@ -28,7 +28,11 @@ class Book {
|
||||
$sql = "
|
||||
SELECT b.*,
|
||||
COUNT(c.id) as chapter_count,
|
||||
COALESCE(SUM(c.word_count), 0) as total_words
|
||||
COALESCE(
|
||||
CASE WHEN b.is_short_story = 1
|
||||
THEN CHAR_LENGTH(b.content) - CHAR_LENGTH(REPLACE(b.content, ' ', '')) + 1
|
||||
ELSE COALESCE(SUM(c.word_count), 0) END, 0
|
||||
) as total_words
|
||||
FROM books b
|
||||
LEFT JOIN chapters c ON b.id = c.book_id
|
||||
WHERE b.user_id = ?
|
||||
@@ -45,10 +49,12 @@ class Book {
|
||||
public function create($data) {
|
||||
$share_token = bin2hex(random_bytes(16));
|
||||
$published = isset($data['published']) ? (int)$data['published'] : 0;
|
||||
|
||||
$is_short_story = isset($data['is_short_story']) ? (int)$data['is_short_story'] : 0;
|
||||
$content = $is_short_story ? ($data['content'] ?? null) : null;
|
||||
|
||||
$stmt = $this->pdo->prepare("
|
||||
INSERT INTO books (title, description, genre, user_id, series_id, sort_order_in_series, share_token, published)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
|
||||
INSERT INTO books (title, description, genre, user_id, series_id, sort_order_in_series, share_token, published, is_short_story, content)
|
||||
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
");
|
||||
return $stmt->execute([
|
||||
$data['title'],
|
||||
@@ -58,12 +64,16 @@ class Book {
|
||||
$data['series_id'] ?? null,
|
||||
$data['sort_order_in_series'] ?? null,
|
||||
$share_token,
|
||||
$published
|
||||
$published,
|
||||
$is_short_story,
|
||||
$content
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
public function update($id, $data) {
|
||||
$published = isset($data['published']) ? (int)$data['published'] : 0;
|
||||
$is_short_story = isset($data['is_short_story']) ? (int)$data['is_short_story'] : 0;
|
||||
$content = $is_short_story ? ($data['content'] ?? null) : null;
|
||||
|
||||
// Преобразуем пустые строки в NULL для integer полей
|
||||
$series_id = !empty($data['series_id']) ? (int)$data['series_id'] : null;
|
||||
@@ -71,7 +81,7 @@ class Book {
|
||||
|
||||
$stmt = $this->pdo->prepare("
|
||||
UPDATE books
|
||||
SET title = ?, description = ?, genre = ?, series_id = ?, sort_order_in_series = ?, published = ?
|
||||
SET title = ?, description = ?, genre = ?, series_id = ?, sort_order_in_series = ?, published = ?, is_short_story = ?, content = ?
|
||||
WHERE id = ? AND user_id = ?
|
||||
");
|
||||
return $stmt->execute([
|
||||
@@ -81,6 +91,8 @@ class Book {
|
||||
$series_id, // Теперь это либо integer, либо NULL
|
||||
$sort_order_in_series, // Теперь это либо integer, либо NULL
|
||||
$published,
|
||||
$is_short_story,
|
||||
$content,
|
||||
$id,
|
||||
$data['user_id']
|
||||
]);
|
||||
@@ -200,17 +212,17 @@ class Book {
|
||||
public function getBookStats($book_id, $only_published_chapters = false) {
|
||||
$sql = "
|
||||
SELECT
|
||||
COUNT(c.id) as chapter_count,
|
||||
COALESCE(SUM(c.word_count), 0) as total_words
|
||||
b.is_short_story,
|
||||
(SELECT COUNT(*) FROM chapters c WHERE c.book_id = b.id " . ($only_published_chapters ? "AND c.status = 'published'" : "") . ") as chapter_count,
|
||||
CASE
|
||||
WHEN b.is_short_story = 1 AND b.content IS NOT NULL THEN CHAR_LENGTH(b.content) - CHAR_LENGTH(REPLACE(b.content, ' ', '')) + 1
|
||||
WHEN b.is_short_story = 0 THEN (SELECT COALESCE(SUM(word_count), 0) FROM chapters WHERE book_id = b.id " . ($only_published_chapters ? "AND status = 'published'" : "") . ")
|
||||
ELSE 0
|
||||
END as total_words
|
||||
FROM books b
|
||||
LEFT JOIN chapters c ON b.id = c.book_id
|
||||
WHERE b.id = ?
|
||||
";
|
||||
|
||||
if ($only_published_chapters) {
|
||||
$sql .= " AND c.status = 'published'";
|
||||
}
|
||||
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute([$book_id]);
|
||||
return $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
|
||||
+27
-15
@@ -8,16 +8,21 @@ class Series {
|
||||
$this->pdo = $pdo;
|
||||
}
|
||||
|
||||
public function findById($id) {
|
||||
public function findById($id) {
|
||||
$stmt = $this->pdo->prepare("
|
||||
SELECT s.*,
|
||||
COUNT(b.id) as book_count,
|
||||
COALESCE((
|
||||
SELECT SUM(c.word_count)
|
||||
FROM chapters c
|
||||
JOIN books b2 ON c.book_id = b2.id
|
||||
(
|
||||
SELECT COALESCE(SUM(
|
||||
CASE
|
||||
WHEN b2.is_short_story = 1
|
||||
THEN CHAR_LENGTH(b2.content) - CHAR_LENGTH(REPLACE(b2.content, ' ', '')) + 1
|
||||
ELSE (SELECT COALESCE(SUM(word_count), 0) FROM chapters WHERE book_id = b2.id)
|
||||
END
|
||||
), 0)
|
||||
FROM books b2
|
||||
WHERE b2.series_id = s.id AND b2.published = 1
|
||||
), 0) as total_words
|
||||
) as total_words
|
||||
FROM series s
|
||||
LEFT JOIN books b ON s.id = b.series_id AND b.published = 1
|
||||
WHERE s.id = ?
|
||||
@@ -25,18 +30,19 @@ class Series {
|
||||
");
|
||||
$stmt->execute([$id]);
|
||||
return $stmt->fetch(PDO::FETCH_ASSOC);
|
||||
}
|
||||
}
|
||||
|
||||
public function findByUser($user_id, $include_stats = true) {
|
||||
if ($include_stats) {
|
||||
$sql = "
|
||||
SELECT s.*,
|
||||
COUNT(b.id) as book_count,
|
||||
COALESCE((
|
||||
SELECT SUM(c.word_count)
|
||||
FROM chapters c
|
||||
JOIN books b2 ON c.book_id = b2.id
|
||||
WHERE b2.series_id = s.id AND b2.user_id = ?
|
||||
COALESCE(SUM(
|
||||
CASE
|
||||
WHEN b.is_short_story = 1
|
||||
THEN CHAR_LENGTH(b.content) - CHAR_LENGTH(REPLACE(b.content, ' ', '')) + 1
|
||||
ELSE (SELECT COALESCE(SUM(word_count), 0) FROM chapters WHERE book_id = b.id)
|
||||
END
|
||||
), 0) as total_words
|
||||
FROM series s
|
||||
LEFT JOIN books b ON s.id = b.series_id
|
||||
@@ -45,7 +51,7 @@ class Series {
|
||||
ORDER BY s.created_at DESC
|
||||
";
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
$stmt->execute([$user_id, $user_id]);
|
||||
$stmt->execute([$user_id]);
|
||||
} else {
|
||||
$sql = "SELECT * FROM series WHERE user_id = ? ORDER BY created_at DESC";
|
||||
$stmt = $this->pdo->prepare($sql);
|
||||
@@ -128,8 +134,14 @@ class Series {
|
||||
$sql = "
|
||||
SELECT
|
||||
COUNT(b.id) as book_count,
|
||||
COALESCE(SUM(stats.chapter_count), 0) as chapter_count,
|
||||
COALESCE(SUM(stats.total_words), 0) as total_words
|
||||
COALESCE(SUM(
|
||||
CASE WHEN b.is_short_story = 1 THEN 0 ELSE stats.chapter_count END
|
||||
), 0) as chapter_count,
|
||||
COALESCE(SUM(
|
||||
CASE WHEN b.is_short_story = 1
|
||||
THEN CHAR_LENGTH(b.content) - CHAR_LENGTH(REPLACE(b.content, ' ', '')) + 1
|
||||
ELSE COALESCE(stats.total_words, 0) END
|
||||
), 0) as total_words
|
||||
FROM series s
|
||||
LEFT JOIN books b ON s.id = b.series_id
|
||||
LEFT JOIN (
|
||||
|
||||
Reference in New Issue
Block a user