feat: Add endpoint /api/v1/blogs
This commit is contained in:
parent
7d75847cd8
commit
59560d7c44
33
app/api/endpoints/api/v1/blogs.py
Normal file
33
app/api/endpoints/api/v1/blogs.py
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
@app.post("/blogs/", response_model=dict)
|
||||||
|
def create_blog(blog: Blog):
|
||||||
|
global blog_id_counter
|
||||||
|
new_blog = {"id": blog_id_counter, "title": blog.title, "content": blog.content}
|
||||||
|
blogs.append(new_blog)
|
||||||
|
blog_id_counter += 1
|
||||||
|
return {"message": "Blog created successfully", "blog": new_blog}
|
||||||
|
|
||||||
|
@app.get("/blogs/", response_model=List[dict])
|
||||||
|
def get_blogs():
|
||||||
|
return blogs
|
||||||
|
|
||||||
|
@app.get("/blogs/{blog_id}", response_model=dict)
|
||||||
|
def get_blog(blog_id: int):
|
||||||
|
for blog in blogs:
|
||||||
|
if blog["id"] == blog_id:
|
||||||
|
return blog
|
||||||
|
raise HTTPException(status_code=404, detail="Blog not found")
|
||||||
|
|
||||||
|
@app.put("/blogs/{blog_id}", response_model=dict)
|
||||||
|
def update_blog(blog_id: int, updated_blog: Blog):
|
||||||
|
for blog in blogs:
|
||||||
|
if blog["id"] == blog_id:
|
||||||
|
blog["title"] = updated_blog.title
|
||||||
|
blog["content"] = updated_blog.content
|
||||||
|
return {"message": "Blog updated successfully", "blog": blog}
|
||||||
|
raise HTTPException(status_code=404, detail="Blog not found")
|
||||||
|
|
||||||
|
@app.delete("/blogs/{blog_id}", response_model=dict)
|
||||||
|
def delete_blog(blog_id: int):
|
||||||
|
global blogs
|
||||||
|
blogs = [blog for blog in blogs if blog["id"] != blog_id]
|
||||||
|
return {"message": "Blog deleted successfully"}
|
Loading…
x
Reference in New Issue
Block a user