Core Spark (C# Crud Generation)

C# Crud Generation

C# CRUD Generation

Create full CRUD for a User Entity with username, first name, last name, email, phone, and start date


Responses From Spark Variants that Implement the Core Spark (C# CRUD Generation)

Sure, here is the complete C# code for a User entity with full CRUD operations as requested:

1. Entity Class

using System;
using System.ComponentModel.DataAnnotations;

namespace YourNamespace.Entities
{
    /// <summary>
    /// Represents a User entity.
    /// </summary>
    public class User
    {
        [Key]
        public int Id { get; set; }

        [Required]
        [Display(Name = "Username")]
        public string Username { get; set; }

        [Required]
        [Display(Name = "First Name")]
        public string FirstName { get; set; }

        [Required]
        [Display(Name = "Last Name")]
        public string LastName { get; set; }

        [Required]
        [Display(Name = "Email")]
        public string Email { get; set; }

        [Required]
        [Display(Name = "Phone")]
        public string Phone { get; set; }

        [Required]
        [Display(Name = "Start Date")]
        public DateTime StartDate { get; set; } = DateTime.Now;

        [Display(Name = "Created Date")]
        public DateTime CreatedDate { get; set; } = DateTime.Now;

        [Display(Name = "Updated Date")]
        public DateTime UpdatedDate { get; set; } = DateTime.Now;
    }
}

2. Data Transfer Object (DTO) Class

using System;
using System.ComponentModel.DataAnnotations;

namespace YourNamespace.DTOs
{
    /// <summary>
    /// Represents a User Data Transfer Object.
    /// </summary>
    public class UserDto
    {
        [Display(Name = "Username")]
        public string Username { get; set; } = "Username";

        [Display(Name = "First Name")]
        public string FirstName { get; set; } = "FirstName";

        [Display(Name = "Last Name")]
        public string LastName { get; set; } = "LastName";

        [Display(Name = "Email")]
        public string Email { get; set; } = "Email";

        [Display(Name = "Phone")]
        public string Phone { get; set; } = "Phone";

        [Display(Name = "Start Date")]
        public DateTime StartDate { get; set; } = DateTime.Now;
    }
}

3. Data Context Class

using Microsoft.EntityFrameworkCore;
using YourNamespace.Entities;

namespace YourNamespace.Data
{
    public class AppDbContext : DbContext
    {
        public DbSet<User> Users { get; set; }

        public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<User>()
                .HasIndex(u => u.Username)
                .IsUnique();
        }
    }
}

4. Mapper Static Class

using YourNamespace.Entities;
using YourNamespace.DTOs;

namespace YourNamespace.Mappers
{
    public static class UserMapper
    {
        public static UserDto ToDto(this User user)
        {
            return new UserDto
            {
                Username = user.Username,
                FirstName = user.FirstName,
                LastName = user.LastName,
                Email = user.Email,
                Phone = user.Phone,
                StartDate = user.StartDate
            };
        }

        public static User ToEntity(this UserDto userDto)
        {
            return new User
            {
                Username = userDto.Username,
                FirstName = userDto.FirstName,
                LastName = userDto.LastName,
                Email = userDto.Email,
                Phone = userDto.Phone,
                StartDate = userDto.StartDate
            };
        }
    }
}

5. Service Class

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using YourNamespace.Data;
using YourNamespace.DTOs;
using YourNamespace.Entities;
using YourNamespace.Mappers;

namespace YourNamespace.Services
{
    public class UserService
    {
        private readonly AppDbContext _context;

        public UserService(AppDbContext context)
        {
            _context = context;
        }

        public async Task<UserDto> Upsert(UserDto userDto)
        {
            var user = await _context.Users
                .FirstOrDefaultAsync(u => u.Username == userDto.Username);

            if (user == null)
            {
                user = userDto.ToEntity();
                _context.Users.Add(user);
            }
            else
            {
                user.FirstName = userDto.FirstName;
                user.LastName = userDto.LastName;
                user.Email = userDto.Email;
                user.Phone = userDto.Phone;
                user.StartDate = userDto.StartDate;
                user.UpdatedDate = DateTime.Now;
            }

            await _context.SaveChangesAsync();
            return user.ToDto();
        }

        public async Task<UserDto> ReadOne(int id)
        {
            var user = await _context.Users.FindAsync(id);
            return user?.ToDto();
        }

        public async Task<IEnumerable<UserDto>> ReadAll()
        {
            return await _context.Users
                .Select(u => u.ToDto())
                .ToListAsync();
        }

        public async Task<bool> Delete(int id)
        {
            var user = await _context.Users.FindAsync(id);
            if (user == null)
            {
                return false;
            }

            _context.Users.Remove(user);
            await _context.SaveChangesAsync();
            return true;
        }
    }
}

6. MVC Controller

using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using YourNamespace.DTOs;
using YourNamespace.Services;

namespace YourNamespace.Controllers
{
    public class UsersController : Controller
    {
        private readonly UserService _userService;

        public UsersController(UserService userService)
        {
            _userService = userService;
        }

        public async Task<IActionResult> Index()
        {
            var users = await _userService.ReadAll();
            return View(users);
        }

        public async Task<IActionResult> Details(int id)
        {
            var user = await _userService.ReadOne(id);
            if (user == null)
            {
                return NotFound();
            }

            return View(user);
        }

        public IActionResult Edit(int id)
        {
            return View(new UserDto());
        }

        [HttpPost]
        public async Task<IActionResult> Edit(int id, UserDto userDto)
        {
            if (ModelState.IsValid)
            {
                await _userService.Upsert(userDto);
                return RedirectToAction(nameof(Index));
            }

            return View(userDto);
        }

        public async Task<IActionResult> Delete(int id)
        {
            var user = await _userService.ReadOne(id);
            if (user == null)
            {
                return NotFound();
            }

            return View(user);
        }

        [HttpPost, ActionName("Delete")]
        public async Task<IActionResult> DeleteConfirmed(int id)
        {
            await _userService.Delete(id);
            return RedirectToAction(nameof(Index));
        }
    }
}

7. MVC Razor Views

Index.cshtml

@model IEnumerable<YourNamespace.DTOs.UserDto>

@{
    ViewData["Title"] = "Users";
}

<h1>Users</h1>

<table class="table">
    <thead>
        <tr>
            <th>Username</th>
            <th>First Name</th>
            <th>Last Name</th>
            <th>Email</th>
            <th>Phone</th>
            <th>Start Date</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var user in Model)
        {
            <tr>
                <td>@user.Username</td>
                <td>@user.FirstName</td>
                <td>@user.LastName</td>
                <td>@user.Email</td>
                <td>@user.Phone</td>
                <td>@user.StartDate</td>
                <td>
                    <a asp-action="Details" asp-route-id="@user.Id">Details</a> |
                    <a asp-action="Edit" asp-route-id="@user.Id">Edit</a> |
                    <a asp-action="Delete" asp-route-id="@user.Id">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

Details.cshtml

@model YourNamespace.DTOs.UserDto

@{
    ViewData["Title"] = "User Details";
}

<h1>User Details</h1>

<div>
    <h4>User</h4>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">Username</dt>
        <dd class="col-sm-10">@Model.Username</dd>
        <dt class="col-sm-2">First Name</dt>
        <dd class="col-sm-10">@Model.FirstName</dd>
        <dt class="col-sm-2">Last Name</dt>
        <dd class="col-sm-10">@Model.LastName</dd>
        <dt class="col-sm-2">Email</dt>
        <dd class="col-sm-10">@Model.Email</dd>
        <dt class="col-sm-2">Phone</dt>
        <dd class="col-sm-10">@Model.Phone</dd>
        <dt class="col-sm-2">Start Date</dt>
        <dd class="col-sm-10">@Model.StartDate</dd>
    </dl>
</div>
<div>
    <a asp-action="Edit" asp-route-id="@Model.Id">Edit</a> |
    <a asp-action="Index">Back to List</a>
</div>

Edit.cshtml

@model YourNamespace.DTOs.UserDto

@{
    ViewData["Title"] = "Edit User";
}

<h1>Edit User</h1>

<h4>User</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div class="form-group">
                <label asp-for="Username" class="control-label"></label>
                <input asp-for="Username" class="form-control" />
                <span asp-validation-for="Username" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="FirstName" class="control-label"></label>
                <input asp-for="FirstName" class="form-control" />
                <span asp-validation-for="FirstName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="LastName" class="control-label"></label>
                <input asp-for="LastName" class="form-control" />
                <span asp-validation-for="LastName" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Email" class="control-label"></label>
                <input asp-for="Email" class="form-control" />
                <span asp-validation-for="Email" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Phone" class="control-label"></label>
                <input asp-for="Phone" class="form-control" />
                <span asp-validation-for="Phone" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="StartDate" class="control-label"></label>
                <input asp-for="StartDate" class="form-control" />
                <span asp-validation-for="StartDate" class="text-danger"></span>
            </div>
            <div class="form-group">
                <input type="submit" value="Save" class="btn btn-primary" />
            </div>
        </form>
    </div>
</div>
<div>
    <a asp-action="Index">Back to List</a>
</div>

Delete.cshtml

@model YourNamespace.DTOs.UserDto

@{
    ViewData["Title"] = "Delete User";
}

<h1>Delete User</h1>

<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>User</h4>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">Username</dt>
        <dd class="col-sm-10">@Model.Username</dd>
        <dt class="col-sm-2">First Name</dt>
        <dd class="col-sm-10">@Model.FirstName</dd>
        <dt class="col-sm-2">Last Name</dt>
        <dd class="col-sm-10">@Model.LastName</dd>
        <dt class="col-sm-2">Email</dt>
        <dd class="col-sm-10">@Model.Email</dd>
        <dt class="col-sm-2">Phone</dt>
        <dd class="col-sm-10">@Model.Phone</dd>
        <dt class="col-sm-2">Start Date</dt>
        <dd class="col-sm-10">@Model.StartDate</dd>
    </dl>
</div>
<form asp-action="DeleteConfirmed">
    <input type="hidden" asp-for="Id" />
    <input type="submit" value="Delete" class="btn btn-danger" /> |
    <a asp-action="Index">Back to List</a>
</form>

This code provides a full CRUD implementation for a User entity, including the necessary Entity, DTO, Data Context, Mapper, Service, Controller, and Razor Views.