diff --git a/app/api/endpoints/api/v1/blogs.py b/app/api/endpoints/api/v1/blogs.py new file mode 100644 index 0000000..433ae1a --- /dev/null +++ b/app/api/endpoints/api/v1/blogs.py @@ -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"} \ No newline at end of file