Skip to content

lcsmota/StoreCRUD

Repository files navigation

StoreCRUD

Gravacao.de.tela.de.07-04-2023.11.53.24.webm

🌐 Status

Finished project βœ…

🧰 Prerequisites

  • .NET 6.0 or +

  • Connection string to SQLServer in StoreCRUD/appsettings.json named as Default

Database

Create a database in SQLServer that contains the table created from the following script:

CREATE TABLE [Categories] (
    [Id] int NOT NULL IDENTITY,
    [Title] nvarchar(80) NOT NULL,
    CONSTRAINT [PK_Categories] PRIMARY KEY ([Id])
);
GO

CREATE TABLE [Users] (
    [Id] int NOT NULL IDENTITY,
    [UserName] nvarchar(80) NOT NULL,
    [Password] nvarchar(max) NOT NULL,
    [Role] nvarchar(max) NOT NULL,
    CONSTRAINT [PK_Users] PRIMARY KEY ([Id])
);
GO

CREATE TABLE [Products] (
    [Id] int NOT NULL IDENTITY,
    [Title] nvarchar(80) NOT NULL,
    [Description] nvarchar(255) NOT NULL,
    [Price] decimal(18,2) NOT NULL,
    [CategoryId] int NOT NULL,
    CONSTRAINT [PK_Products] PRIMARY KEY ([Id]),
    CONSTRAINT [FK_Products_Categories_CategoryId] FOREIGN KEY ([CategoryId]) REFERENCES [Categories] ([Id]) ON DELETE CASCADE
);
GO

Relationships

+--------------+        +-------------+        +--------------+
|   Categories | 1    * |    Products |        |     Users    |
+--------------+        +-------------+        +--------------+
|     Id       |<-------|      Id     |        |      Id      |
|     Title    |        |     Title   |        |   UserName   |
|              |        | Description |        |   Password   |
|              |        |    Price    |        |     Role     |
+--------------+        |  CategoryId |        +--------------+
                        +-------------+

πŸ”§ Installation

$ git clone https://github.com/lcsmota/StoreCRUD.git

$ cd StoreCRUD/

$ dotnet restore

$ dotnet run

Server listenning at https://localhost:7195/swagger or https://localhost:7195/api/v1/Users, https://localhost:7195/api/v1/Products and https://localhost:7195/api/v1/Categories

πŸ“« Routes for Login

Return all objects (Users)

  GET https://localhost:7195/api/v1/Users

βš™οΈ Status Code:

  (200) - OK
  (401) - Unauthorized

πŸ“¬ Postman

πŸ“ Swagger

Return only one object (User)

  GET https://localhost:7195/api/v1/Users/${id}
Parameter Type Description
id int Mandatory. The ID of the object you want to view

βš™οΈ Status Code:

  (200) - OK
  (401) - Unauthorized
  (404) - Not Found

πŸ“¬ Postman

πŸ“ Swagger

Insert a new object (User)

  POST https://localhost:7195/api/v1/Users

πŸ“¨ body:

{
  "userName": "maria",
  "password": "maria123456"
}

🧾 response:

{
  "id": 1002,
  "userName": "maria",
  "password": "",
  "role": "employee"
}

βš™οΈ Status Code:

  (200) - OK
  (400) - Bad Request

πŸ“¬ Postman

πŸ“ Swagger

Login (User)

  POST https://localhost:7195/api/v1/Users/login

πŸ“¨ body:

{
  "userName": "maria",
  "password": "M@r1a*&$123"
}

🧾 response:

{
  "user": {
    "id": 1002,
    "userName": "maria",
    "password": "",
    "role": "employee"
  },
  "token": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1laWQiOiIxMDAyIiwidW5pcXVlX25hbWUiOiJtYXJpYSIsInJvbGUiOiJlbXBsb3llZSIsIm5iZiI6MTY4MDgyOTk2MCwiZXhwIjoxNjgwODMxMTYwLCJpYXQiOjE2ODA4Mjk5NjB9.F4U_Xq5IX3nK8L0jnfy7FFYz0Alir_YM_RPRxK4RckE"
}

βš™οΈ Status Code:

  (200) - OK
  (404) - Not Found

πŸ“¬ Postman

πŸ“ Swagger

Update an object (User)

  PUT https://localhost:7195/api/v1/Users/${id}
Parameter Type Description
id int Mandatory. The ID of the object you want to update

πŸ“¨ body:

{
  "id": 1002,
  "userName": "maria",
  "password": "M@r1a*&$123",
  "role": "employee"
}

🧾 response:

βš™οΈ Status Code:

  (204) - No Content
  (400) - Bad Request
  (401) - Unauthorized

πŸ“¬ Postman

πŸ“ Swagger

πŸ“« Routes for Products

Return all objects (Products)

  GET https://localhost:7195/api/v1/Products

βš™οΈ Status Code:

  (200) - OK

πŸ“¬ Postman

πŸ“ Swagger

Return only one object (Product)

  GET https://localhost:7195/api/v1/Products/${id}
Parameter Type Description
id int Mandatory. The ID of the object you want to view

βš™οΈ Status Code:

  (200) - OK
  (404) - Not Found

πŸ“¬ Postman

πŸ“ Swagger

Return products with category

  GET https://localhost:7195/api/v1/Products/productsWithCateg

βš™οΈ Status Code:

  (200) - OK

πŸ“¬ Postman

πŸ“ Swagger

Return product with category

  GET https://localhost:7195/api/v1/Products/${id}/productsWithCateg
Parameter Type Description
id int Mandatory. The ID of the object you want to view

βš™οΈ Status Code:

  (200) - OK
  (404) - Not Found

πŸ“¬ Postman

πŸ“ Swagger

Return category with product

  GET https://localhost:7195/api/v1/Products/categories/${id}
Parameter Type Description
id int Mandatory. The ID of the object you want to view

βš™οΈ Status Code:

  (200) - OK

πŸ“¬ Postman

πŸ“ Swagger

Insert a new object (Product)

  POST https://localhost:7195/api/v1/Products

πŸ“¨ body:

{
  "title": "C# Basics",
  "description": "Learn C# Fundamentals by Coding",
  "price": 150,
  "categoryId": 1
}

🧾 response:

{
    "id": 3002,
    "title": "C# Basics",
    "description": "Learn C# Fundamentals by Coding",
    "price": 150,
    "categoryId": 1
}

βš™οΈ Status Code:

  (201) - Created
  (400) - Bad Request

πŸ“¬ Postman

πŸ“ Swagger

Update an object (Product)

  PUT https://localhost:7195/api/v1/Products/${id}
Parameter Type Description
id int Mandatory. The ID of the object you want to update

πŸ“¨ body:

{
  "id": 3002,
  "title": "C# 10 Basics",
  "description": "Learn C# Fundamentals by Coding",
  "price": 360,
  "categoryId": 1,
  "category": {
    "id": 1,
    "title": "Course"
  }
}

🧾 response:

βš™οΈ Status Code:

  (204) - No Content
  (400) - Bad Request
  (401) - Unauthorized

πŸ“¬ Postman

πŸ“ Swagger

Delete an object (Product)

  DELETE https://localhost:7195/api/v1/Products/${id}
Parameter Type Description
id int Mandatory. The ID of the object you want to delete

πŸ“¨ body:

🧾 response:

βš™οΈ Status Code:

  (204) - No Content
  (401) - Unauthorized
  (404) - Not Found
  (400) - Bad Request

πŸ“¬ Postman

πŸ“ Swagger

πŸ“« Routes for Categories

Return all objects (Categories)

  GET https://localhost:7195/api/v1/Categories

βš™οΈ Status Code:

  (200) - OK

πŸ“¬ Postman

πŸ“ Swagger

Return only one object (Category)

  GET https://localhost:7195/api/v1/Categories/${id}
Parameter Type Description
id int Mandatory. The ID of the object you want to view

βš™οΈ Status Code:

  (200) - OK
  (404) - Not Found

πŸ“¬ Postman

πŸ“ Swagger

Insert a new object (Category)

  POST https://localhost:7195/api/v1/Categories

πŸ“¨ body:

{
  "title": "Bootcamp"
}

🧾 response:

{
    "id": 2005,
    "title": "Bootcamp"
}

βš™οΈ Status Code:

  (200) - Ok
  (400) - Bad Request
  (401) - Unauthorized

πŸ“¬ Postman

πŸ“ Swagger

Update an object (Category)

  PUT https://localhost:7195/api/v1/Categories/${id}
Parameter Type Description
id int Mandatory. The ID of the object you want to update

πŸ“¨ body:

{
  "id": "2005",
  "title": "Bootcamps"
}

🧾 response:

βš™οΈ Status Code:

  (204) - No Content
  (400) - Bad Request
  (401) - Unauthorized

πŸ“¬ Postman

πŸ“ Swagger

Delete an object (Category)

  DELETE https://localhost:7195/api/v1/Categories/${id}
Parameter Type Description
id int Mandatory. The ID of the object you want to delete

πŸ“¨ body:

🧾 response:

βš™οΈ Status Code:

  (204) - No Content
  (404) - Not Found
  (401) - Unauthorized
  (400) - Bad Request

πŸ“¬ Postman

πŸ“ Swagger

πŸ”¨ Tools used

πŸ–₯️ Technologies and practices used

  • C# 10
  • .NET CORE 6
  • SQL SERVER
  • Entity Framework 7
  • Code First
  • Token JWT
  • Swagger
  • DTOs
  • Dependency injection
  • POO

πŸ“– Features

Registration, Listing, Update and Removal