Skip to content

Commit

Permalink
Sample data seeding implementation (#101)
Browse files Browse the repository at this point in the history
  • Loading branch information
Ben0189 authored Oct 2, 2024
1 parent 1409af7 commit 5668dd0
Show file tree
Hide file tree
Showing 10 changed files with 619 additions and 143 deletions.
30 changes: 4 additions & 26 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,21 @@ The primary goal of BlotzTaskApp is to provide users with a powerful yet easy-to
BlotzTaskApp utilizes a variety of modern technologies to ensure a robust, scalable, and maintainable codebase:

### Frontend
- Library: React.js
- Framework: Next.js
- CSS Framework: Tailwind CSS
- Component Library: MUI
- Design - Shadcn

### Backend
- Framework: .NET
- Framework: .NET 8
- API: REST API

### Data Layer
- ORM: Entity Framework Core
- Database: SQL Server

### Testing
- UI/Component Testing: Cypress, Playwright
- Unit/API Testing: RestSharp, XUnit

### Cloud Service & Hosting
- Repository: GitHub
- CI/CD: GitHub Actions
- Backend Hosting: Azure
- Frontend Hosting: Vercel
- Web Service: Azure SQL Database, Web App
- Monitoring: Application Insights

## Features

- **Task Creation**: Easily create and manage tasks.
- **Task Categorization**: Organize tasks into categories.
- **Due Dates and Reminders**: Set deadlines and receive reminders.
- **Progress Tracking**: Monitor the progress of tasks.
- **Responsive Design**: Use the app on any device, thanks to a responsive design.

## Installation

To set up BlotzTaskApp locally, follow these steps:

1. **Clone the repository:**
```bash
git clone https://github.com/yourusername/BlotzTaskApp.git
cd BlotzTaskApp
- Web Service: Azure SQL Database, Azure Web App, Azure Key Vault
1 change: 0 additions & 1 deletion blotztask-api/Controllers/LabelController.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using BlotzTask.Models;
using BlotzTask.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace BlotzTask.Controllers
Expand Down
55 changes: 0 additions & 55 deletions blotztask-api/Data/BlotzTaskDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,61 +14,6 @@ public BlotzTaskDbContext(DbContextOptions options) : base(options) { }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<TaskItem>().HasData(
new TaskItem
{
Id = 1,
Title = "Initial Task 1",
Description = "Description for Task 1",
DueDate = new DateOnly(2024, 10, 01),
IsDone = false,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
},
new TaskItem
{
Id = 2,
Title = "Initial Task 2",
Description = "Description for Task 2",
DueDate = new DateOnly(2024, 10, 01),
IsDone = true,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
},
new TaskItem
{
Id = 3,
Title = "Initial Task 3",
Description = "Description for Task 3",
DueDate = new DateOnly(2024, 10, 01),
IsDone = false,
CreatedAt = DateTime.UtcNow,
UpdatedAt = DateTime.UtcNow
}
);
modelBuilder.Entity<Label>().HasData(
new Label
{
LabelId = 1,
Name = "Urgent",
Color = "Red",
Description = "Tasks that need to be addressed immediately"
},
new Label
{
LabelId = 2,
Name = "In Progress",
Color = "Yellow",
Description = "Tasks that are currently being worked on"
},
new Label
{
LabelId = 3,
Name = "Completed",
Color = "Green",
Description = "Tasks that have been completed"
}
);
}
}
}
5 changes: 4 additions & 1 deletion blotztask-api/Data/Entities/Label.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
namespace BlotzTask.Data.Entities
using System.ComponentModel.DataAnnotations.Schema;

namespace BlotzTask.Data.Entities
{
public class Label
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int LabelId { get; set; }
public string Name { get; set; } = string.Empty;
public string Color { get; set; } = string.Empty;
Expand Down
4 changes: 4 additions & 0 deletions blotztask-api/Data/Entities/TaskItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@ public class TaskItem
public bool IsDone { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
public string UserId { get; set; }

Check warning on line 16 in blotztask-api/Data/Entities/TaskItem.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'UserId' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.
[ForeignKey("UserId")]
public User User { get; set; }

Check warning on line 18 in blotztask-api/Data/Entities/TaskItem.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'User' must contain a non-null value when exiting constructor. Consider adding the 'required' modifier or declaring the property as nullable.

}
}
102 changes: 102 additions & 0 deletions blotztask-api/Data/Seeding/BlotzContextSeed.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
using System.Security.Claims;
using BlotzTask.Data;
using BlotzTask.Data.Entities;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;

public static class BlotzContextSeed
{
public static async Task SeedBlotzContextAsync(UserManager<User> userManager, RoleManager<IdentityRole> roleManager, BlotzTaskDbContext context)
{
// Seed roles
if (!await roleManager.RoleExistsAsync("Admin"))
{
await roleManager.CreateAsync(new IdentityRole("Admin"));
}

// Seed admin user
var defaultUser = new User
{
UserName = "blotztest1",
Email = "blotztest1@gmail.com",
EmailConfirmed = true,
};

var user = await userManager.FindByEmailAsync(defaultUser.Email);

// If the user already exists, log and return early
if (user != null)
{
Console.WriteLine($"User with email {defaultUser.Email} already exists. No new user was added. No task and label was added");
return; // Exit early since no further seeding is needed
}

// If the user does not exist, create the new super admin
var createUserResult = await userManager.CreateAsync(defaultUser, "@Blotztest1");
if (createUserResult.Succeeded)
{
await userManager.AddToRoleAsync(defaultUser, "Admin");

var userAdded = await userManager.FindByEmailAsync(defaultUser.Email);

List<Claim> claims = new List<Claim>
{
new Claim("CanEdit", "true"),
new Claim("CanPost", "true"),
new Claim("CanDelete", "true")
};

await userManager.AddClaimsAsync(userAdded, claims);

Check warning on line 49 in blotztask-api/Data/Seeding/BlotzContextSeed.cs

View workflow job for this annotation

GitHub Actions / build

Possible null reference argument for parameter 'user' in 'Task<IdentityResult> UserManager<User>.AddClaimsAsync(User user, IEnumerable<Claim> claims)'.
user = userAdded;
Console.WriteLine("User creation Success.");
}
else
{
Console.WriteLine("User creation failed.");
return; // Exit early if user creation failed
}

// Seed labels and tasks only if the user was successfully created
if (!await context.Labels.AnyAsync())
{
await context.Labels.AddAsync(
new Label
{
Name = "Urgent",
Color = "Red",
Description = "Tasks that need to be addressed immediately"
}
);
await context.SaveChangesAsync();
Console.WriteLine("Label creation Success.");
}

if (!await context.TaskItems.AnyAsync())
{
await context.TaskItems.AddRangeAsync(
new TaskItem
{
Title = "Initial Task 1",
Description = "Description for Task 1",
DueDate = new DateOnly(2024, 10, 01),
IsDone = false,
CreatedAt = new DateTime(2024, 10, 2),
UpdatedAt = new DateTime(2024, 10, 2),
UserId = user.Id
},
new TaskItem
{
Title = "Initial Task 2",
Description = "Description for Task 2",
DueDate = new DateOnly(2024, 10, 01),
IsDone = true,
CreatedAt = new DateTime(2024, 10, 2),
UpdatedAt = new DateTime(2024, 10, 2),
UserId = user.Id
}
);
await context.SaveChangesAsync();
Console.WriteLine("Tasks creation Success.");
}
}
}
Loading

0 comments on commit 5668dd0

Please sign in to comment.