-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblog.py
More file actions
235 lines (202 loc) · 6.23 KB
/
blog.py
File metadata and controls
235 lines (202 loc) · 6.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from sqlalchemy import not_
from sqlalchemy.orm import Session
from api.v1.models.blog import Blog
from api.v1.schemas.blog import (
BlogCreate,
BlogListItemResponse,
BlogListResponse,
BlogResponse,
BlogUpdate,
)
class BlogService:
"""Service class for blog operations."""
@staticmethod
def create_blog(
db: Session,
blog: BlogCreate,
) -> BlogResponse:
"""Create a new blog post.
Args:
db (Session): The database session.
blog (BlogCreate): The blog post data to create.
Returns:
BlogResponse: The created blog post response.
Raises:
ValueError: If a blog post with the same title already exists.
"""
existing_blog = db.query(Blog).filter(Blog.title == blog.title).first()
if existing_blog:
raise ValueError("A blog post with this title already exists.")
new_blog = Blog(
title=blog.title,
excerpt=blog.excerpt,
content=blog.content,
image_url=blog.image_url,
)
db.add(new_blog)
db.commit()
db.refresh(new_blog)
return BlogResponse(
id=new_blog.id,
title=new_blog.title,
excerpt=new_blog.excerpt,
content=new_blog.content,
image_url=new_blog.image_url,
created_at=new_blog.created_at,
updated_at=new_blog.updated_at,
)
@staticmethod
def list_blog(
db: Session,
page: int,
page_size: int,
) -> BlogListResponse:
"""Retrieve a list of blog posts with pagination.
Args:
db (Session): The database session.
page (int): The page number for pagination.
page_size (int): The number of items per page.
Returns:
BlogListResponse: The list of blog posts with pagination info.
"""
offset = (page - 1) * page_size
query = (
db.query(Blog)
.filter(not_(Blog.is_deleted))
.order_by(Blog.created_at.desc())
)
total_count = query.count()
blogs = query.offset(offset).limit(page_size).all()
next_page = (
f"/api/v1/blogs?page={page + 1}&page_size={page_size}"
if offset + page_size < total_count
else None
)
prev_page = (
f"/api/v1/blogs?page={page - 1}&page_size={page_size}" if page > 1 else None
)
results = [
BlogListItemResponse(
id=blog.id,
title=blog.title,
excerpt=blog.excerpt,
image_url=blog.image_url,
created_at=blog.created_at,
)
for blog in blogs
]
return BlogListResponse(
count=total_count,
next=next_page,
previous=prev_page,
results=results,
)
@staticmethod
def read_blog(
db: Session,
id: int,
) -> BlogResponse:
"""Retrieve a blog post by ID.
Args:
db (Session): The database session.
id (int): The ID of the blog post to retrieve.
Returns:
BlogResponse: The blog post response.
Raises:
ValueError: If the blog post is not found.
"""
blog = (
db.query(Blog)
.filter(
Blog.id == id,
not_(Blog.is_deleted),
)
.first()
)
if not blog:
raise ValueError("Blog post not found.")
return BlogResponse(
id=blog.id,
title=blog.title,
excerpt=blog.excerpt,
content=blog.content,
image_url=blog.image_url,
created_at=blog.created_at,
updated_at=blog.updated_at,
)
@staticmethod
def update_blog(
db: Session,
id: int,
blog_update: BlogUpdate,
) -> BlogResponse:
"""Update an existing blog post by ID.
Args:
db (Session): The database session.
id (int): The ID of the blog post to update.
blog_update (BlogUpdate): The updated blog post data.
Returns:
BlogResponse: The updated blog post response.
Raises:
ValueError: If the blog post is not found or a blog post
with the updated title already exists.
"""
blog = (
db.query(Blog)
.filter(
Blog.id == id,
not_(Blog.is_deleted),
)
.first()
)
if not blog:
raise ValueError("Blog post not found.")
update_data = blog_update.model_dump(exclude_unset=True)
if "title" in update_data and update_data["title"] != blog.title:
existing_blog = (
db.query(Blog)
.filter(
Blog.title == update_data["title"],
not_(Blog.is_deleted),
)
.first()
)
if existing_blog:
raise ValueError("A blog post with this title already exists.")
for field, value in update_data.items():
setattr(blog, field, value)
db.commit()
db.refresh(blog)
return BlogResponse(
id=blog.id,
title=blog.title,
excerpt=blog.excerpt,
content=blog.content,
image_url=blog.image_url,
created_at=blog.created_at,
updated_at=blog.updated_at,
)
@staticmethod
def delete_blog(
db: Session,
id: int,
) -> None:
"""Delete a blog post by ID (soft delete).
Args:
db (Session): The database session.
id (int): The ID of the blog post to delete.
Raises:
ValueError: If the blog post is not found.
"""
blog_to_delete = (
db.query(Blog)
.filter(
Blog.id == id,
not_(Blog.is_deleted),
)
.first()
)
if not blog_to_delete:
raise ValueError("Blog post not found.")
blog_to_delete.is_deleted = True
db.commit()