Core Spark (C# Crud Generation)

C# Crud Generation

C# CRUD Generation

Create a implementation of a workout tracker table, include name, date, time, duration, exercise type, and comment


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

Sure, I'll provide you with the complete implementation for a workout tracker table. This will include the Entity class, DTO class, Data Context, Mapper, Service, MVC Controller, and Razor Views.

1. Entity Class

using System;
using System.ComponentModel.DataAnnotations;

namespace WorkoutTracker.Models
{
    public class Workout
    {
        [Key]
        public int Id { get; set; }

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

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

        [Required]
        [Display(Name = "Time")]
        public TimeSpan Time { get; set; }

        [Required]
        [Display(Name = "Duration")]
        public TimeSpan Duration { get; set; }

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

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

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

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

2. DTO Class

using System;
using System.ComponentModel.DataAnnotations;

namespace WorkoutTracker.DTOs
{
    public class WorkoutDto
    {
        [Display(Name = "Workout Name")]
        public string Name { get; set; } = "Name";

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

        [Display(Name = "Time")]
        public TimeSpan Time { get; set; }

        [Display(Name = "Duration")]
        public TimeSpan Duration { get; set; }

        [Display(Name = "Exercise Type")]
        public string ExerciseType { get; set; } = "ExerciseType";

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

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

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

3. Data Context Class

using Microsoft.EntityFrameworkCore;
using WorkoutTracker.Models;

namespace WorkoutTracker.Data
{
    public class WorkoutContext : DbContext
    {
        public WorkoutContext(DbContextOptions<WorkoutContext> options) : base(options) { }

        public DbSet<Workout> Workouts { get; set; }

        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Workout>()
                .HasIndex(w => w.Name)
                .IsUnique();
        }
    }
}

4. Mapper Static Class

using WorkoutTracker.DTOs;
using WorkoutTracker.Models;

namespace WorkoutTracker.Mappers
{
    public static class WorkoutMapper
    {
        public static WorkoutDto ToDto(this Workout workout)
        {
            return new WorkoutDto
            {
                Name = workout.Name,
                Date = workout.Date,
                Time = workout.Time,
                Duration = workout.Duration,
                ExerciseType = workout.ExerciseType,
                Comment = workout.Comment,
                CreatedDate = workout.CreatedDate,
                UpdatedDate = workout.UpdatedDate
            };
        }

        public static Workout ToEntity(this WorkoutDto workoutDto)
        {
            return new Workout
            {
                Name = workoutDto.Name,
                Date = workoutDto.Date,
                Time = workoutDto.Time,
                Duration = workoutDto.Duration,
                ExerciseType = workoutDto.ExerciseType,
                Comment = workoutDto.Comment,
                CreatedDate = workoutDto.CreatedDate,
                UpdatedDate = workoutDto.UpdatedDate
            };
        }
    }
}

5. Service Class

using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.EntityFrameworkCore;
using WorkoutTracker.Data;
using WorkoutTracker.DTOs;
using WorkoutTracker.Mappers;
using WorkoutTracker.Models;

namespace WorkoutTracker.Services
{
    public class WorkoutService
    {
        private readonly WorkoutContext _context;

        public WorkoutService(WorkoutContext context)
        {
            _context = context;
        }

        public async Task<WorkoutDto> UpsertAsync(WorkoutDto workoutDto)
        {
            var workout = await _context.Workouts
                .FirstOrDefaultAsync(w => w.Name == workoutDto.Name);

            if (workout == null)
            {
                workout = workoutDto.ToEntity();
                _context.Workouts.Add(workout);
            }
            else
            {
                workout.Date = workoutDto.Date;
                workout.Time = workoutDto.Time;
                workout.Duration = workoutDto.Duration;
                workout.ExerciseType = workoutDto.ExerciseType;
                workout.Comment = workoutDto.Comment;
                workout.UpdatedDate = DateTime.Now;
                _context.Workouts.Update(workout);
            }

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

        public async Task<WorkoutDto> ReadOneAsync(int id)
        {
            var workout = await _context.Workouts.FindAsync(id);
            return workout?.ToDto();
        }

        public async Task<IEnumerable<WorkoutDto>> ReadAllAsync()
        {
            return await _context.Workouts
                .Select(w => w.ToDto())
                .ToListAsync();
        }

        public async Task<bool> DeleteAsync(int id)
        {
            var workout = await _context.Workouts.FindAsync(id);
            if (workout == null)
            {
                return false;
            }

            _context.Workouts.Remove(workout);
            await _context.SaveChangesAsync();
            return true;
        }
    }
}

6. MVC Controller

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

namespace WorkoutTracker.Controllers
{
    public class WorkoutsController : Controller
    {
        private readonly WorkoutService _workoutService;

        public WorkoutsController(WorkoutService workoutService)
        {
            _workoutService = workoutService;
        }

        public async Task<IActionResult> Index()
        {
            var workouts = await _workoutService.ReadAllAsync();
            return View(workouts);
        }

        public async Task<IActionResult> Details(int id)
        {
            var workout = await _workoutService.ReadOneAsync(id);
            if (workout == null)
            {
                return NotFound();
            }
            return View(workout);
        }

        public IActionResult Edit(int? id)
        {
            if (id == null)
            {
                return View(new WorkoutDto());
            }
            else
            {
                var workout = _workoutService.ReadOneAsync(id.Value).Result;
                if (workout == null)
                {
                    return NotFound();
                }
                return View(workout);
            }
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Edit(int id, WorkoutDto workoutDto)
        {
            if (ModelState.IsValid)
            {
                await _workoutService.UpsertAsync(workoutDto);
                return RedirectToAction(nameof(Index));
            }
            return View(workoutDto);
        }

        public async Task<IActionResult> Delete(int id)
        {
            var workout = await _workoutService.ReadOneAsync(id);
            if (workout == null)
            {
                return NotFound();
            }
            return View(workout);
        }

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

7. MVC Razor Views

Index.cshtml

@model IEnumerable<WorkoutTracker.DTOs.WorkoutDto>

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

<h1>Workouts</h1>

<p>
    <a asp-action="Edit">Create New</a>
</p>

<table class="table">
    <thead>
        <tr>
            <th>Name</th>
            <th>Date</th>
            <th>Time</th>
            <th>Duration</th>
            <th>Exercise Type</th>
            <th>Comment</th>
            <th></th>
        </tr>
    </thead>
    <tbody>
        @foreach (var item in Model)
        {
            <tr>
                <td>@item.Name</td>
                <td>@item.Date.ToShortDateString()</td>
                <td>@item.Time</td>
                <td>@item.Duration</td>
                <td>@item.ExerciseType</td>
                <td>@item.Comment</td>
                <td>
                    <a asp-action="Edit" asp-route-id="@item.Id">Edit</a> |
                    <a asp-action="Details" asp-route-id="@item.Id">Details</a> |
                    <a asp-action="Delete" asp-route-id="@item.Id">Delete</a>
                </td>
            </tr>
        }
    </tbody>
</table>

Details.cshtml

@model WorkoutTracker.DTOs.WorkoutDto

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

<h1>Workout Details</h1>

<div>
    <h4>Workout</h4>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">
            Name
        </dt>
        <dd class="col-sm-10">
            @Model.Name
        </dd>
        <dt class="col-sm-2">
            Date
        </dt>
        <dd class="col-sm-10">
            @Model.Date.ToShortDateString()
        </dd>
        <dt class="col-sm-2">
            Time
        </dt>
        <dd class="col-sm-10">
            @Model.Time
        </dd>
        <dt class="col-sm-2">
            Duration
        </dt>
        <dd class="col-sm-10">
            @Model.Duration
        </dd>
        <dt class="col-sm-2">
            Exercise Type
        </dt>
        <dd class="col-sm-10">
            @Model.ExerciseType
        </dd>
        <dt class="col-sm-2">
            Comment
        </dt>
        <dd class="col-sm-10">
            @Model.Comment
        </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 WorkoutTracker.DTOs.WorkoutDto

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

<h1>Edit Workout</h1>

<h4>Workout</h4>
<hr />
<div class="row">
    <div class="col-md-4">
        <form asp-action="Edit">
            <div class="form-group">
                <label asp-for="Name" class="control-label"></label>
                <input asp-for="Name" class="form-control" />
                <span asp-validation-for="Name" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Date" class="control-label"></label>
                <input asp-for="Date" class="form-control" />
                <span asp-validation-for="Date" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Time" class="control-label"></label>
                <input asp-for="Time" class="form-control" />
                <span asp-validation-for="Time" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Duration" class="control-label"></label>
                <input asp-for="Duration" class="form-control" />
                <span asp-validation-for="Duration" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="ExerciseType" class="control-label"></label>
                <input asp-for="ExerciseType" class="form-control" />
                <span asp-validation-for="ExerciseType" class="text-danger"></span>
            </div>
            <div class="form-group">
                <label asp-for="Comment" class="control-label"></label>
                <input asp-for="Comment" class="form-control" />
                <span asp-validation-for="Comment" 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 WorkoutTracker.DTOs.WorkoutDto

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

<h1>Delete Workout</h1>

<h3>Are you sure you want to delete this?</h3>
<div>
    <h4>Workout</h4>
    <hr />
    <dl class="row">
        <dt class="col-sm-2">
            Name
        </dt>
        <dd class="col-sm-10">
            @Model.Name
        </dd>
        <dt class="col-sm-2">
            Date
        </dt>
        <dd class="col-sm-10">
            @Model.Date.ToShortDateString()
        </dd>
        <dt class="col-sm-2">
            Time
        </dt>
        <dd class="col-sm-10">
            @Model.Time
        </dd>
        <dt class="col-sm-2">
            Duration
        </dt>
        <dd class="col-sm-10">
            @Model.Duration
        </dd>
        <dt class="col-sm-2">
            Exercise Type
        </dt>
        <dd class="col-sm-10">
            @Model.ExerciseType
        </dd>
        <dt class="col-sm-2">
            Comment
        </dt>
        <dd class="col-sm-10">
            @Model.Comment
        </dd>
    </dl>
    
    <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>
</div>

This complete implementation includes the entity class, DTO class, data context, mapper, service, MVC controller, and Razor views for a workout tracker table. You can now integrate this into your .NET Core 8 project.