Coder Social home page Coder Social logo

blogapi-project's Introduction

Blog API Project

master

Summary

The Blog API project is a simple Blog API without any Authentication support. Built using an SQLite memory as storage the Blog API can provide a backend service to create blogs, posts, and comments for other websites.

A Blog

A blog is a collection of posts with a name and description.

A Post

is a part of a Blog. It has a title, summary, and a content. Note the planned implementation only accepts text and doesn't support media or file uploading.

A Comment

All comments are attached to a Post. Each comment has a username field and a content section. Comments are limitted to only 200 characters.

What this Project Contains

This project was developed using industry standard Test Driven Development in C#. It contains three main projects:

  • A Core project built originally as a .NET 6 Class Library, although it also references ASP.NET Libraries when needed.
  • A Application project built on top of ASP.NET 6 using a API Controller and Model Framework. The project doesn't create any views for the website. Instead Swagger is used to provide a frontend for testing.
  • A Storage project built on top of Entity Framework Core 6 and SQLite using the repository pattern. SQLite is implemented using an inMemmory database following Microsoft's reccomendations for InMemmory Database. And three Test Projects
  • Application.Tests
  • Domain.Tests
  • Storage.Tests Each test namespace uses the same namespace as the project they test. The project was built with TDD in mind with tests designed before features were implemented. The Storage and Domain Tests are both Unit Tests. However The Application project mostly uses intergration tests to test Validation Logic and API support.

Using the Project

The database handles 5 similar requests for Blogs, Posts, and Comments:

  • GET Request. Takes a valid ID returns the object connected to that ID.
  • GET List Request. Lists a collection of Blogs, Posts, or Comments. For Comments Get List requires a valid Post ID and Post Requires a valid Blog ID. This means when a website requests a List of Posts they will only get the Posts connected with their websites blog. (Rather then all websites). Note Get List for blogs returns all blogs.
  • Post Request Takes an object constructed in Json, checks to make sure the object is valid, and then, if valid, creates a new Database entry and returns a location value for the new database object.
  • Post Update Request takes a different object from Post Request. It requires a valid ID, and then validates the object. If valid it will update a databases entry.
  • Delete Request takes a valid database ID and deletes the model requestesd. This is a hard delete. Note: Blog Delete also deletes any Posts Connected to it. Post also deletes any connected Comments. The reason this happens is because this API doesn't support free standing Posts or Comments. Posts are expected to be apart of a Blog and Comments are about a Post. It wouldn't make sense for Posts or Comments to exist by themselves.

Creating a Blog

image

Using Swagger you can easily fill in the Request body to create a valid request. The Blog requires a Name and Summary to be provided. The name doesn't have to be unique.

Upon hitting execute you should get back the following: image

The location value provides a URL for a get request. You can either paste it into another window, or copy the ID and use the Get request to validate its successful implementation if you so wish.

Creating a Post

Using the Blog ID, provided by creating a blog, you can easily create a new post for your blog. Copy the BlogID into the BlogID field. Then fill out the post. image

Upon hitting execute you should get back the following: image

Web Applications can then use the Get Command to get the data needed to display a post on a webpage. Date Created and Date Modified are automatically created by the Web API and don't accept user input.

Like the Post, the Location Header provides a url querry that can retrieve the Post.

Getting a List of Post

image

By using your blog Id you can retrive all posts you create for your blog. This means other websites don't need to store each post ID in order to display them from the user. Only the blog ID.

Example result: image

Creating A Comment.

In order to create a Comment to a post the user needs to provide the Post ID of the user they are commenting on. In this API, because their is no authentication, the username must be provided. image

Response back: image

Getting a List of Comments

image By using your post Id you can retrive all posts you create for your blog. This means other websites don't need to store each comment ID in order to display them from the user. Only the post ID is requred, which can be retreived from a post list. image

Updating your Comment

Updating a Comment, or Post, or Blog, requires the ID of the object being created and passing in an Update request. image

Note: Updating expects different values from creating a post. Thats because this API only supports modifying some of the content of a comment. For example content like usernames shouldn't change as their related to the identity of the comment creator, althought the comment itself can be editted easily. In the event that you want to change a comments username you can delete the comment the comment and create a new one.

image

Also the server automatically updated the DateModified value to reflect the new edited changes.

Deleting a Blog/Comment/Post

Deleting is just as easy as creating new content. In this api everything is hard deleted and can't be retrieved once deleted. Because Comments are apart of Posts, which are apart of Blogs, deleting a Blog will also delete all Posts connected to it. Deleting a Post will also delete its comments. There is no point in the database storing Posts for a blog that no longer exists or comments on Post that no longer exist.

So an easy way to delete the stuff we just created is to delete the blog. image

Copyright

All Rights Reserved

blogapi-project's People

Contributors

jackw2as avatar

Watchers

 avatar

blogapi-project's Issues

Ensure Data Model works correctly

Ensure that each model validates correctly and can be added to a mock Repository. Also, ensure that invalid models don't work. Finally, ensure that the Models Pass correctly to Controllers from the Database.

Tasks:

  • Testing Suite
  • Implement the Models
  • Refactor

Support CRUD for BaseController

Using the MVC pattern ensures that the BaseController works as expected. Note: View won't be created in this project.

Each Event should take a Response and Return a Request. Thus it should use the Response/Request Pattern.

Must Support:

  • Create
  • Update (Edit)
  • Read
  • Delete

WIP:

  • Build a Test suite
  • Implement Test suite
  • Refactor

Issue with deleting request

Rework the way deleting works.

Plan: let each controller handle deleting. Allow controllers to talk to one another to request deleting. Eg blog controller, when deleting a blog will first call the post controller. Which will then call the comment controller.

Second Plan: Remove all links from Blog to Posts and Posts to comments. The link should only be in one direction.

Build the Storage Database.

The database is constructed using a Code First Approach in the. Storage project. In order to implement the database, a simple repository pattern was used to communicate with the database. The repository was built as a Generic Repository that provides a basic implementation.

The Database for this project will be using a SQLite in-memory database.

In the future, if a specific model requires a more specific touch or feature the InMemory Repository can be used as a parent class with its functionality wrapped for a specific model.

Decided to use a Repository Pattern to manage the database.

  • Created a Test suite
  • Implemented said Test suite#
  • Refactor it.

Validation Check

Ensure that only valid models are passed to the Repository.

Ensure that invalid models are caught by the Controller and BadRequest is returned with the model errors.

Must ensure the following validates correctly:

  • Blog - Create
  • Blog - Update
  • Post - Create
  • Post - Update
  • Comment - Create
  • Comment - Update

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.