create a gofundme clone in csharp

To create a GoFundMe clone in C#, you can follow these steps:

  1. Create a new ASP.NET Core web application in Visual Studio, choosing the Web Application template.

  2. Add required packages for MVC, Razor, Entity Framework, and SQL Server. You can do this by adding the following packages to your project:

main.cs
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Microsoft.EntityFrameworkCore.Design
dotnet add package Microsoft.EntityFrameworkCore.SqlServer
dotnet add package Microsoft.EntityFrameworkCore.Tools
dotnet add package Microsoft.VisualStudio.Web.CodeGeneration.Design
287 chars
6 lines
  1. Create the database schema and models. You can use the following models:
main.cs
public class User
{
    public int UserId { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
}

public class Campaign
{
    public int CampaignId { get; set; }
    public string Title { get; set; }
    public string Description { get; set; }
    public decimal GoalAmount { get; set; }
    public decimal AmountRaised { get; set; }
    public DateTime CreatedAt { get; set; }
    public DateTime Deadline { get; set; }
    public User User { get; set; }
    public int UserId { get; set; }
}
530 chars
20 lines
  1. Create the database context. You can use the following context:
main.cs
public class GoFundMeContext : DbContext
{
    public GoFundMeContext(DbContextOptions<GoFundMeContext> options)
        : base(options)
    {
    }

    public DbSet<User> Users { get; set; }
    public DbSet<Campaign> Campaigns { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Campaign>()
            .HasOne(c => c.User)
            .WithMany(u => u.Campaigns)
            .HasForeignKey(c => c.UserId)
            .OnDelete(DeleteBehavior.Cascade);
    }
}
532 chars
20 lines
  1. Create the views and controllers for creating, updating, and viewing campaigns. You can use the following actions and views:

CampaignController.cs

main.cs
public class CampaignController : Controller
{
    private readonly GoFundMeContext _context;

    public CampaignController(GoFundMeContext context)
    {
        _context = context;
    }

    public IActionResult Index()
    {
        var campaigns = _context.Campaigns
            .Include(c => c.User)
            .ToList();

        return View(campaigns);
    }

    public IActionResult Create()
    {
        return View();
    }

    [HttpPost]
    public IActionResult Create(Campaign campaign)
    {
        campaign.CreatedAt = DateTime.Now;
        campaign.AmountRaised = 0;

        _context.Campaigns.Add(campaign);
        _context.SaveChanges();

        return RedirectToAction(nameof(Index));
    }

    public IActionResult Edit(int id)
    {
        var campaign = _context.Campaigns.Find(id);

        if (campaign == null)
        {
            return NotFound();
        }

        return View(campaign);
    }

    [HttpPost]
    public IActionResult Edit(Campaign campaign)
    {
        _context.Update(campaign);
        _context.SaveChanges();

        return RedirectToAction(nameof(Index));
    }

    public IActionResult Details(int id)
    {
        var campaign = _context.Campaigns
            .Include(c => c.User)
            .FirstOrDefault(c => c.CampaignId == id);

        if (campaign == null)
        {
            return NotFound();
        }

        return View(campaign);
    }
}
1430 chars
71 lines

Create.cshtml

@model Campaign

<h2>Create new campaign</h2>

<form asp-action="Create">
    <div class="form-group">
        <label>Title</label>
        <input asp-for="Title" class="form-control" />
    </div>
    <div class="form-group">
        <label>Description</label>
        <textarea asp-for="Description" class="form-control"></textarea>
    </div>
    <div class="form-group">
        <label>Goal amount</label>
        <input asp-for="GoalAmount" class="form-control" />
    </div>
    <div class="form-group">
        <label>Deadline</label>
        <input asp-for="Deadline" type="date" class="form-control" />
    </div>
    <button type="submit" class="btn btn-primary">Create</button>
</form>
697 chars
24 lines

Edit.cshtml

@model Campaign

<h2>Edit campaign</h2>

<form asp-action="Edit">
    <input type="hidden" asp-for="CampaignId" />
    <div class="form-group">
        <label>Title</label>
        <input asp-for="Title" class="form-control" />
    </div>
    <div class="form-group">
        <label>Description</label>
        <textarea asp-for="Description" class="form-control"></textarea>
    </div>
    <div class="form-group">
        <label>Goal amount</label>
        <input asp-for="GoalAmount" class="form-control" />
    </div>
    <div class="form-group">
        <label>Deadline</label>
        <input asp-for="Deadline" type="date" class="form-control" />
    </div>
    <button type="submit" class="btn btn-primary">Save</button>
</form>
736 chars
25 lines

Index.cshtml

@model List<Campaign>

<h2>Campaigns</h2>

<div class="row">
    @foreach (var campaign in Model)
    {
        <div class="col-md-4">
            <div class="card">
                <div class="card-body">
                    <h5 class="card-title">@campaign.Title</h5>
                    <p class="card-text">@campaign.Description</p>
                    <ul class="list-group list-group-flush">
                        <li class="list-group-item">Amount raised: $@campaign.AmountRaised</li>
                        <li class="list-group-item">Goal amount: $@campaign.GoalAmount</li>
                        <li class="list-group-item">Deadline: @campaign.Deadline.ToShortDateString()</li>
                    </ul>
                    <a href="/Campaign/Details/@campaign.CampaignId" class="btn btn-primary">View</a>
                </div>
            </div>
        </div>
    }
</div>
890 chars
24 lines

Details.cshtml

@model Campaign

<h2>@Model.Title</h2>

<p>@Model.Description</p>

<ul class="list-group list-group-flush">
    <li class="list-group-item">Amount raised: $@Model.AmountRaised</li>
    <li class="list-group-item">Goal amount: $@Model.GoalAmount</li>
    <li class="list-group-item">Deadline: @Model.Deadline.ToShortDateString()</li>
    <li class="list-group-item">Created by: @Model.User.Name</li>
</ul>

<a href="/Campaign/Edit/@Model.CampaignId" class="btn btn-primary">Edit</a>
482 chars
15 lines
  1. Finally, run the application and start creating campaigns! This is just a basic example, so you can add more features like authentication, donations, and campaign updates.

gistlibby LogSnag