19. Building a Full-Stack Project: Part 1
3 min readJun 28, 2024
Project Setup and Planning
We will create a task manager application where users can create, view, and manage tasks. This project will have two main parts:
- Backend: A .NET Core API for managing tasks.
- Frontend: A React application for interacting with the API.
Designing the Application Architecture
- Backend (API):
- Task model with properties:
Id
,Title
,Description
,IsCompleted
, andDueDate
. - Endpoints for CRUD operations.
- Database integration using Entity Framework Core with MySQL.
2. Frontend (React):
- Components: TaskList, TaskForm, TaskItem.
- Fetch tasks from the API.
- Create new tasks and mark them as completed.
Setting Up the Backend with .NET
Step 1: Create a New ASP.NET Core Web API Project
- Open Visual Studio and create a new project.
- Select
ASP.NET Core Web API
, name itTaskManagerAPI
, and configure it to use .NET 6.0.
Step 2: Add NuGet Packages
- Install Entity Framework Core and MySQL provider:
- Right-click on the project, select
Manage NuGet Packages
. - Install
Pomelo.EntityFrameworkCore.MySql
,Microsoft.EntityFrameworkCore
, andMicrosoft.EntityFrameworkCore.Tools
.
Step 3: Create the Data Model
- Add a Models Folder and create a
Task.cs
file:
namespace TaskManagerAPI.Models
{
public class Task
{
public int Id { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public bool IsCompleted { get; set; }
public DateTime DueDate { get; set; }
}
}
Step 4: Create the Database Context
- Add a Data Folder and create a
TaskContext.cs
file:
using Microsoft.EntityFrameworkCore;
using TaskManagerAPI.Models;
namespace TaskManagerAPI.Data
{
public class TaskContext : DbContext
{
public TaskContext(DbContextOptions<TaskContext> options) : base(options) { }
public DbSet<Task> Tasks { get; set; }
}
}
Step 5: Configure Database Connection
- Open
appsettings.json
and add the connection string:
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=taskmanagerdb;User=root;Password=yourpassword;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*"
}
2. Configure Services in Program.cs
:
using Microsoft.EntityFrameworkCore;
using TaskManagerAPI.Data;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddDbContext<TaskContext>(options =>
options.UseMySql(builder.Configuration.GetConnectionString("DefaultConnection"), new MySqlServerVersion(new Version(8, 0, 21))));
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthorization();
app.MapControllers();
app.Run();
Step 6: Create the Controller
- Add a
TasksController.cs
in the Controllers folder:
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using TaskManagerAPI.Data;
using TaskManagerAPI.Models;
namespace TaskManagerAPI.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class TasksController : ControllerBase
{
private readonly TaskContext _context;
public TasksController(TaskContext context)
{
_context = context;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<Task>>> GetTasks()
{
return await _context.Tasks.ToListAsync();
}
[HttpGet("{id}")]
public async Task<ActionResult<Task>> GetTask(int id)
{
var task = await _context.Tasks.FindAsync(id);
if (task == null)
{
return NotFound();
}
return task;
}
[HttpPost]
public async Task<ActionResult<Task>> PostTask(Task task)
{
_context.Tasks.Add(task);
await _context.SaveChangesAsync();
return CreatedAtAction(nameof(GetTask), new { id = task.Id }, task);
}
}
}
Step 7: Enable and Use Swagger for Testing
- Run the Application:
- Press
F5
or click theRun
button in Visual Studio to start the application. - The Swagger UI will be available at
https://localhost:5001/swagger
(or a similar URL).
2. Test the API:
- Use Swagger UI to test the CRUD operations.
GET /api/tasks
to fetch all tasks.GET /api/tasks/{id}
to fetch a specific task.POST /api/tasks
to create a new task.
Step 8: Set Up MySQL Workbench and Migrate Database
- Open MySQL Workbench and connect to your local MySQL server.
- Run Migrations to create the database schema:
- Open the Package Manager Console in Visual Studio (
Tools
->NuGet Package Manager
->Package Manager Console
). - Run the following commands:
Add-Migration InitialCreate
Update-Database
Conclusion of Part 1
We’ve set up the backend of our task manager application using ASP.NET Core with MySQL. The database schema is managed through Entity Framework Core, and we can perform CRUD operations via our API.
More from this Series
This blog is part of my series “Building Dynamic Web Apps: React and .NET Unleashed”. If you found this helpful, be sure to check out the other posts in the series: