Project Introduction
The Personal Finance Management System is designed to help users effectively manage their income, expenses, budgets, and financial goals. Built using ASP.NET and SQL Server, this application provides a user-friendly interface for tracking financial activities, generating reports, and setting financial goals. The system allows users to gain insights into their financial health, make informed decisions, and achieve their financial objectives through careful planning and monitoring.
Project Objectives
- To create a secure user authentication system for managing user accounts and roles.
- To enable users to record and manage their income sources and amounts.
- To facilitate the tracking of expenses by category and date.
- To allow users to set and manage budgets for different expense categories.
- To generate detailed reports on income, expenses, and overall financial health.
- To help users set financial goals and track their progress towards achieving them.
- To manage investments and monitor their current value over time.
- To implement a notification system to alert users about important financial events.
- To provide a backup system for user data to ensure data integrity and security.
- To collect user feedback to continuously improve the application.
Project Modules
- User Management Module: Handles user registration, login, and role management.
- Income Management Module: Allows users to add, edit, and delete income records.
- Expense Management Module: Facilitates the tracking of expenses by category and date.
- Budget Management Module: Enables users to create and manage budgets for different categories.
- Report Generation Module: Generates reports on income, expenses, and financial health.
- Goal Management Module: Allows users to set financial goals and track their progress.
- Investment Management Module: Manages user investments and tracks their current value.
- Notification Module: Sends notifications to users regarding important financial events.
- Backup Module: Manages data backups to ensure user data is secure and recoverable.
- Feedback Module: Collects user feedback to improve the application.
SQL Server Database Tables
-- Create Users Table
CREATE TABLE Users (
UserId INT PRIMARY KEY IDENTITY(1,1),
Username NVARCHAR(50) NOT NULL UNIQUE,
PasswordHash NVARCHAR(256) NOT NULL,
Email NVARCHAR(100) NOT NULL UNIQUE,
FirstName NVARCHAR(50) NOT NULL,
LastName NVARCHAR(50) NOT NULL,
RoleId INT NOT NULL,
CreatedAt DATETIME DEFAULT GETDATE(),
UpdatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (RoleId) REFERENCES Roles(RoleId)
);
-- Create Roles Table
CREATE TABLE Roles (
RoleId INT PRIMARY KEY IDENTITY(1,1),
RoleName NVARCHAR(50) NOT NULL UNIQUE,
CreatedAt DATETIME DEFAULT GETDATE(),
UpdatedAt DATETIME DEFAULT GETDATE()
);
-- Create Income Table
CREATE TABLE Income (
IncomeId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
Amount DECIMAL(18, 2) NOT NULL,
Source NVARCHAR(100) NOT NULL,
DateReceived DATETIME NOT NULL,
CreatedAt DATETIME DEFAULT GETDATE(),
UpdatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (User Id) REFERENCES Users(UserId)
);
-- Create Expense Table
CREATE TABLE Expenses (
ExpenseId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
Amount DECIMAL(18, 2) NOT NULL,
Category NVARCHAR(100) NOT NULL,
DateIncurred DATETIME NOT NULL,
Description NVARCHAR(MAX),
CreatedAt DATETIME DEFAULT GETDATE(),
UpdatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (User Id) REFERENCES Users(UserId)
);
-- Create Budget Table
CREATE TABLE Budgets (
BudgetId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
Category NVARCHAR(100) NOT NULL,
Amount DECIMAL(18, 2) NOT NULL,
StartDate DATETIME NOT NULL,
EndDate DATETIME NOT NULL,
CreatedAt DATETIME DEFAULT GETDATE(),
UpdatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (User Id) REFERENCES Users(UserId)
);
-- Create Reports Table
CREATE TABLE Reports (
ReportId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
ReportDate DATETIME NOT NULL,
ReportContent NVARCHAR(MAX),
CreatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (User Id) REFERENCES Users(UserId)
);
-- Create Goals Table
CREATE TABLE Goals (
GoalId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
GoalName NVARCHAR(100) NOT NULL,
TargetAmount DECIMAL(18, 2) NOT NULL,
CurrentAmount DECIMAL(18, 2) NOT NULL DEFAULT 0,
TargetDate DATETIME NOT NULL,
CreatedAt DATETIME DEFAULT GETDATE(),
UpdatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (User Id) REFERENCES Users(UserId)
);
-- Create Investments Table
CREATE TABLE Investments (
InvestmentId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
InvestmentName NVARCHAR(100) NOT NULL,
Amount DECIMAL(18, 2) NOT NULL,
InvestmentDate DATETIME NOT NULL,
CurrentValue DECIMAL(18, 2) NOT NULL,
CreatedAt DATETIME DEFAULT GETDATE(),
UpdatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (User Id) REFERENCES Users(UserId)
);
-- Create Notifications Table
CREATE TABLE Notifications (
NotificationId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
Message NVARCHAR(255) NOT NULL,
IsRead BIT NOT NULL DEFAULT 0,
CreatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (User Id) REFERENCES Users(UserId)
);
-- Create Backup Table
CREATE TABLE Backups (
BackupId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
BackupDate DATETIME DEFAULT GETDATE(),
BackupFile NVARCHAR(255) NOT NULL,
CreatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (User Id) REFERENCES Users(UserId)
);
-- Create Feedback Table
CREATE TABLE Feedback (
FeedbackId INT PRIMARY KEY IDENTITY(1,1),
UserId INT NOT NULL,
FeedbackContent NVARCHAR(MAX) NOT NULL,
CreatedAt DATETIME DEFAULT GETDATE(),
FOREIGN KEY (User Id) REFERENCES Users(UserId)
);
Explanation of Tables
Users: Stores user information, including their role.
Roles: Defines different roles (e.g., Admin, User) in the system.
Income: Records income details for users, including source and amount.
Expenses: Manages user expenses, categorized by type and amount.
Budgets: Allows users to set and track budgets for different categories.
Reports: Enables users to generate financial reports based on their data.
Goals: Tracks financial goals set by users, including target and current amounts.
Investments: Manages user investments, including their current value and amount invested.
Notifications: Stores notifications for users regarding financial updates and reminders.
Backups: Keeps records of user data backups for recovery purposes.
Feedback: Collects user feedback on the Personal Finance Manager system.
To create a model and repositories using ADO.NET for an ASP.NET application based on the SQL Server tables you've defined
We will follow these steps:
Create Models
Define C# classes that represent each of the tables.
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string PasswordHash { get; set; }
public string Email { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int RoleId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class Role
{
public int RoleId { get; set; }
public string RoleName { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class Income
{
public int IncomeId { get; set; }
public int UserId { get; set; }
public decimal Amount { get; set; }
public string Source { get; set; }
public DateTime DateReceived { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class Expense
{
public int ExpenseId { get; set; }
public int UserId { get; set; }
public decimal Amount { get; set; }
public string Category { get; set; }
public DateTime DateIncurred { get; set; }
public string Description { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class Budget
{
public int BudgetId { get; set; }
public int UserId { get; set; }
public string Category { get; set; }
public decimal Amount { get; set; }
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class Report
{
public int ReportId { get; set; }
public int UserId { get; set; }
public DateTime ReportDate { get; set; }
public string ReportContent { get; set; }
public DateTime CreatedAt { get; set; }
}
public class Goal
{
public int GoalId { get; set; }
public int UserId { get; set; }
public string GoalName { get; set; }
public decimal TargetAmount { get; set; }
public decimal CurrentAmount { get; set; }
public DateTime TargetDate { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class Investment
{
public int InvestmentId { get; set; }
public int UserId { get; set; }
public string InvestmentName { get; set; }
public decimal Amount { get; set; }
public DateTime InvestmentDate { get; set; }
public decimal CurrentValue { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime UpdatedAt { get; set; }
}
public class Notification
{
public int NotificationId { get; set; }
public int UserId { get; set; }
public string Message { get; set; }
public bool IsRead { get; set; }
public DateTime CreatedAt { get; set; }
}
public class Backup
{
public int BackupId { get; set; }
public int UserId { get; set; }
public DateTime BackupDate { get; set; }
public string BackupFile { get; set; }
public DateTime CreatedAt { get; set; }
}
public class Feedback
{
public int FeedbackId { get; set; }
public int UserId { get; set; }
public string FeedbackContent { get; set; }
public DateTime CreatedAt { get; set; }
}
Create Repositories
Next, we will create a generic repository interface and a concrete implementation for each model. Here’s an example of how to implement a repository for the User model:
IRepository Interface
public interface IRepository<T>
{
IEnumerable<T> GetAll();
T GetById(int id);
void Add(T entity);
void Update(T entity);
void Delete(int id);
}
UserRepository Implementation
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
public class UserRepository : IRepository<User>
{
private readonly string _connectionString;
public UserRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<User> GetAll()
{
var users = new List<User>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Users", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
users.Add(new User
{
UserId = (int)reader["User Id"],
Username = reader["Username"].ToString(),
PasswordHash = reader["PasswordHash"].ToString(),
Email = reader["Email"].ToString(),
FirstName = reader["FirstName"].ToString(),
LastName = reader["LastName"].ToString(),
RoleId = (int)reader["RoleId"],
CreatedAt = (DateTime)reader["CreatedAt"],
Updated At = (DateTime)reader["UpdatedAt"]
});
}
}
}
return users;
}
public User GetById(int id)
{
User user = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Users WHERE UserId = @User Id", connection);
command.Parameters.AddWithValue("@User Id", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
user = new User
{
UserId = (int)reader["User Id"],
Username = reader["Username"].ToString(),
PasswordHash = reader["PasswordHash"].ToString(),
Email = reader["Email"].ToString(),
FirstName = reader["FirstName"].ToString(),
LastName = reader["LastName"].ToString(),
RoleId = (int)reader["RoleId"],
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
};
}
}
}
return user;
}
public void Add(User user)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Users (Username, PasswordHash, Email, FirstName, LastName, RoleId) VALUES (@Username, @PasswordHash, @Email, @FirstName, @LastName, @RoleId)", connection);
command.Parameters.AddWithValue("@Username", user.Username);
command.Parameters.AddWithValue("@PasswordHash", user.PasswordHash);
command.Parameters.AddWithValue("@Email", user.Email);
command.Parameters.AddWithValue("@FirstName", user.FirstName);
command.Parameters.AddWithValue("@LastName", user.LastName);
command.Parameters.AddWithValue("@RoleId", user.RoleId);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(User user)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Users SET Username = @Username, PasswordHash = @PasswordHash, Email = @Email, FirstName = @FirstName, LastName = @LastName, RoleId = @RoleId, UpdatedAt = GETDATE() WHERE UserId = @User Id", connection);
command.Parameters.AddWithValue("@User Id", user.UserId);
command.Parameters.AddWithValue("@Username", user.Username);
command.Parameters.AddWithValue("@PasswordHash", user.PasswordHash);
command.Parameters.AddWithValue("@Email", user.Email);
command.Parameters.AddWithValue("@FirstName", user.FirstName);
command.Parameters.AddWithValue("@LastName", user.LastName);
command.Parameters.AddWithValue("@RoleId", user.RoleId);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Users WHERE UserId = @User Id", connection);
command.Parameters.AddWithValue("@User Id", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
This pattern can be replicated for other models such as Role, Income, Expense, etc., by creating corresponding repository classes that implement the IRepository
Sure! Below are the repository implementations for the remaining models
Role, Income, Expense, Budget, Report, Goal, Investment, Notification, Backup, and Feedback. Each repository will follow the same pattern as the User Repository.
RoleRepository
public class RoleRepository : IRepository<Role>
{
private readonly string _connectionString;
public RoleRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Role> GetAll()
{
var roles = new List<Role>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Roles", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
roles.Add(new Role
{
RoleId = (int)reader["RoleId"],
RoleName = reader["RoleName"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
});
}
}
}
return roles;
}
public Role GetById(int id)
{
Role role = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Roles WHERE RoleId = @RoleId", connection);
command.Parameters.AddWithValue("@RoleId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
role = new Role
{
RoleId = (int)reader["RoleId"],
RoleName = reader["RoleName"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
};
}
}
}
return role;
}
public void Add(Role role)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Roles (RoleName) VALUES (@RoleName)", connection);
command.Parameters.AddWithValue("@RoleName", role.RoleName);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Role role)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Roles SET RoleName = @RoleName, UpdatedAt = GETDATE() WHERE RoleId = @RoleId", connection);
command.Parameters.AddWithValue("@RoleId", role.RoleId);
command.Parameters.AddWithValue("@RoleName", role.RoleName);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Roles WHERE RoleId = @RoleId", connection);
command.Parameters.AddWithValue("@RoleId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
IncomeRepository
public class IncomeRepository : IRepository<Income>
{
private readonly string _connectionString;
public IncomeRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Income> GetAll()
{
var incomes = new List<Income>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Income", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
incomes.Add(new Income
{
IncomeId = (int)reader["IncomeId"],
UserId = (int)reader["User Id"],
Amount = (decimal)reader["Amount"],
Source = reader["Source"].ToString(),
DateReceived = (DateTime)reader["DateReceived"],
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
});
}
}
}
return incomes;
}
public Income GetById(int id)
{
Income income = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Income WHERE IncomeId = @IncomeId", connection);
command.Parameters.AddWithValue("@IncomeId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
income = new Income
{
IncomeId = (int)reader["IncomeId"],
UserId = (int)reader["User Id"],
Amount = (decimal)reader["Amount"],
Source = reader["Source"].ToString(),
DateReceived = (DateTime)reader["DateReceived"],
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
};
}
}
}
return income;
}
public void Add(Income income)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Income (User Id, Amount, Source, DateReceived) VALUES (@User Id, @Amount, @Source, @DateReceived)", connection);
command.Parameters.AddWithValue("@User Id", income.UserId);
command.Parameters.AddWithValue("@Amount", income.Amount);
command.Parameters.AddWithValue("@Source", income.Source);
command.Parameters.AddWithValue("@DateReceived", income.DateReceived);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Income income)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Income SET UserId = @User Id, Amount = @Amount, Source = @Source, DateReceived = @DateReceived, UpdatedAt = GETDATE() WHERE IncomeId = @IncomeId", connection);
command.Parameters.AddWithValue("@IncomeId", income.IncomeId);
command.Parameters.AddWithValue("@User Id", income.UserId);
command.Parameters.AddWithValue("@Amount", income.Amount);
command.Parameters.AddWithValue("@Source", income.Source);
command.Parameters.AddWithValue("@DateReceived", income.DateReceived);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Income WHERE IncomeId = @IncomeId", connection);
command.Parameters.AddWithValue("@IncomeId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
ExpenseRepository
public class ExpenseRepository : IRepository<Expense>
{
private readonly string _connectionString;
public ExpenseRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Expense> GetAll()
{
var expenses = new List<Expense>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Expenses", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
expenses.Add(new Expense
{
ExpenseId = (int)reader["ExpenseId"],
UserId = (int)reader["User Id"],
Amount = (decimal)reader["Amount"],
Category = reader["Category"].ToString(),
DateIncurred = (DateTime)reader["DateIncurred"],
Description = reader["Description"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
});
}
}
}
return expenses;
}
public Expense GetById(int id)
{
Expense expense = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Expenses WHERE ExpenseId = @ExpenseId", connection);
command.Parameters.AddWithValue("@ExpenseId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
expense = new Expense
{
ExpenseId = (int)reader["ExpenseId"],
UserId = (int)reader["User Id"],
Amount = (decimal)reader["Amount"],
Category = reader["Category"].ToString(),
DateIncurred = (DateTime)reader["DateIncurred"],
Description = reader["Description"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
};
}
}
}
return expense;
}
public void Add(Expense expense)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Expenses (User Id, Amount, Category, DateIncurred, Description) VALUES (@User Id, @Amount, @Category, @DateIncurred, @Description)", connection);
command.Parameters.AddWithValue("@User Id", expense.UserId);
command.Parameters.AddWithValue("@Amount", expense.Amount);
command.Parameters.AddWithValue("@Category", expense.Category);
command.Parameters.AddWithValue("@DateIncurred", expense.DateIncurred);
command.Parameters.AddWithValue("@Description", expense.Description);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Expense expense)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Expenses SET UserId = @User Id, Amount = @Amount, Category = @Category, DateIncurred = @DateIncurred, Description = @Description, UpdatedAt = GETDATE() WHERE ExpenseId = @ExpenseId", connection);
command.Parameters.AddWithValue("@ExpenseId", expense.ExpenseId);
command.Parameters.AddWithValue("@User Id", expense.UserId);
command.Parameters.AddWithValue("@Amount", expense.Amount);
command.Parameters.AddWithValue("@Category", expense.Category);
command.Parameters.AddWithValue("@DateIncurred", expense.DateIncurred);
command.Parameters.AddWithValue("@Description", expense.Description);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Expenses WHERE ExpenseId = @ExpenseId", connection);
command.Parameters.AddWithValue("@ExpenseId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
BudgetRepository
public class BudgetRepository : IRepository<Budget>
{
private readonly string _connectionString;
public BudgetRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Budget> GetAll()
{
var budgets = new List<Budget>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Budgets", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
budgets.Add(new Budget
{
BudgetId = (int)reader["BudgetId"],
UserId = (int)reader["User Id"],
Category = reader["Category"].ToString(),
Amount = (decimal)reader["Amount"],
StartDate = (DateTime)reader["StartDate"],
EndDate = (DateTime)reader["EndDate"],
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
});
}
}
}
return budgets;
}
public Budget GetById(int id)
{
Budget budget = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Budgets WHERE BudgetId = @BudgetId", connection);
command.Parameters.AddWithValue("@BudgetId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
budget = new Budget
{
BudgetId = (int)reader["BudgetId"],
UserId = (int)reader["User Id"],
Category = reader["Category"].ToString(),
Amount = (decimal)reader["Amount"],
StartDate = (DateTime)reader["StartDate"],
EndDate = (DateTime)reader["EndDate"],
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
};
}
}
}
return budget;
}
public void Add(Budget budget)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Budgets (User Id, Category, Amount, StartDate, EndDate) VALUES (@User Id, @Category, @Amount, @StartDate, @EndDate)", connection);
command.Parameters.AddWithValue("@User Id", budget.UserId);
command.Parameters.AddWithValue("@Category", budget.Category);
command.Parameters.AddWithValue("@Amount", budget.Amount);
command.Parameters.AddWithValue("@StartDate", budget.StartDate);
command.Parameters.AddWithValue("@EndDate", budget.EndDate);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Budget budget)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Budgets SET UserId = @User Id, Category = @Category, Amount = @Amount, StartDate = @StartDate, EndDate = @EndDate, UpdatedAt = GETDATE() WHERE BudgetId = @BudgetId", connection);
command.Parameters.AddWithValue("@BudgetId", budget.BudgetId);
command.Parameters.AddWithValue("@User Id", budget.UserId);
command.Parameters.AddWithValue("@Category", budget.Category);
command.Parameters.AddWithValue("@Amount", budget.Amount);
command.Parameters.AddWithValue("@StartDate", budget.StartDate);
command.Parameters.AddWithValue("@EndDate", budget.EndDate);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Budgets WHERE BudgetId = @BudgetId", connection);
command.Parameters.AddWithValue("@BudgetId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
ReportRepository
public class ReportRepository : IRepository<Report>
{
private readonly string _connectionString;
public ReportRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Report> GetAll()
{
var reports = new List<Report>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Reports", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
reports.Add(new Report
{
ReportId = (int)reader["ReportId"],
UserId = (int)reader["User Id"],
ReportDate = (DateTime)reader["ReportDate"],
ReportContent = reader["ReportContent"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"]
});
}
}
}
return reports;
}
public Report GetById(int id)
{
Report report = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Reports WHERE ReportId = @ReportId", connection);
command.Parameters.AddWithValue("@ReportId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
report = new Report
{
ReportId = (int)reader["ReportId"],
UserId = (int)reader["UserId"],
ReportDate = (DateTime)reader["ReportDate"],
ReportContent = reader["ReportContent"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"]
};
}
}
}
return report;
}
public void Add(Report report)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Reports (UserId, ReportDate, ReportContent) VALUES (@UserId, @ReportDate, @ReportContent)", connection);
command.Parameters.AddWithValue("@UserId", report.UserId);
command.Parameters.AddWithValue("@ReportDate", report.ReportDate);
command.Parameters.AddWithValue("@ReportContent", report.ReportContent);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Report report)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Reports SET UserId = @UserId, ReportDate = @ReportDate, ReportContent = @ReportContent, CreatedAt = GETDATE() WHERE ReportId = @ReportId", connection);
command.Parameters.AddWithValue("@ReportId", report.ReportId);
command.Parameters.AddWithValue("@UserId", report.UserId);
command.Parameters.AddWithValue("@ReportDate", report.ReportDate);
command.Parameters.AddWithValue("@ReportContent", report.ReportContent);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Reports WHERE ReportId = @ReportId", connection);
command.Parameters.AddWithValue("@ReportId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
GoalRepository
public class GoalRepository : IRepository<Goal>
{
private readonly string _connectionString;
public GoalRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Goal> GetAll()
{
var goals = new List<Goal>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Goals", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
goals.Add(new Goal
{
GoalId = (int)reader["GoalId"],
UserId = (int)reader["UserId"],
GoalName = reader["GoalName"].ToString(),
TargetAmount = (decimal)reader["TargetAmount"],
CurrentAmount = (decimal)reader["CurrentAmount"],
TargetDate = (DateTime)reader["TargetDate"],
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
});
}
}
}
return goals;
}
public Goal GetById(int id)
{
Goal goal = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Goals WHERE GoalId = @GoalId", connection);
command.Parameters.AddWithValue("@GoalId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
goal = new Goal
{
GoalId = (int)reader["GoalId"],
UserId = (int)reader["UserId"],
GoalName = reader["GoalName"].ToString(),
TargetAmount = (decimal)reader["TargetAmount"],
CurrentAmount = (decimal)reader["CurrentAmount"],
TargetDate = (DateTime)reader["TargetDate"],
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
};
}
}
}
return goal;
}
public void Add(Goal goal)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Goals (User Id, GoalName, TargetAmount, CurrentAmount, TargetDate) VALUES (@User Id, @GoalName, @TargetAmount, @CurrentAmount, @TargetDate)", connection);
command.Parameters.AddWithValue("@User Id", goal.UserId);
command.Parameters.AddWithValue("@GoalName", goal.GoalName);
command.Parameters.AddWithValue("@TargetAmount", goal.TargetAmount);
command.Parameters.AddWithValue("@CurrentAmount", goal.CurrentAmount);
command.Parameters.AddWithValue("@TargetDate", goal.TargetDate);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Goal goal)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Goals SET UserId = @User Id, GoalName = @GoalName, TargetAmount = @TargetAmount, CurrentAmount = @CurrentAmount, TargetDate = @TargetDate, UpdatedAt = GETDATE() WHERE GoalId = @GoalId", connection);
command.Parameters.AddWithValue("@GoalId", goal.GoalId);
command.Parameters.AddWithValue("@User Id", goal.UserId);
command.Parameters.AddWithValue("@GoalName", goal.GoalName);
command.Parameters.AddWithValue("@TargetAmount", goal.TargetAmount);
command.Parameters.AddWithValue("@CurrentAmount", goal.CurrentAmount);
command.Parameters.AddWithValue("@TargetDate", goal.TargetDate);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Goals WHERE GoalId = @GoalId", connection);
command.Parameters.AddWithValue("@GoalId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
InvestmentRepository
public class InvestmentRepository : IRepository<Investment>
{
private readonly string _connectionString;
public InvestmentRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Investment> GetAll()
{
var investments = new List<Investment>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Investments", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
investments.Add(new Investment
{
InvestmentId = (int)reader["InvestmentId"],
UserId = (int)reader["User Id"],
InvestmentName = reader["InvestmentName"].ToString(),
Amount = (decimal)reader["Amount"],
InvestmentDate = (DateTime)reader["InvestmentDate"],
CurrentValue = (decimal)reader["CurrentValue"],
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
});
}
}
}
return investments;
}
public Investment GetById(int id)
{
Investment investment = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Investments WHERE InvestmentId = @InvestmentId", connection);
command.Parameters.AddWithValue("@InvestmentId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
investment = new Investment
{
InvestmentId = (int)reader["InvestmentId"],
UserId = (int)reader["User Id"],
InvestmentName = reader["InvestmentName"].ToString(),
Amount = (decimal)reader["Amount"],
InvestmentDate = (DateTime)reader["InvestmentDate"],
CurrentValue = (decimal)reader["CurrentValue"],
CreatedAt = (DateTime)reader["CreatedAt"],
UpdatedAt = (DateTime)reader["UpdatedAt"]
};
}
}
}
return investment;
}
public void Add(Investment investment)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Investments (User Id, InvestmentName, Amount, InvestmentDate, CurrentValue) VALUES (@User Id, @InvestmentName, @Amount, @InvestmentDate, @CurrentValue)", connection);
command.Parameters.AddWithValue("@User Id", investment.UserId);
command.Parameters.AddWithValue("@InvestmentName", investment.InvestmentName);
command.Parameters.AddWithValue("@Amount", investment.Amount);
command.Parameters.AddWithValue("@InvestmentDate", investment.InvestmentDate);
command.Parameters.AddWithValue("@CurrentValue", investment.CurrentValue);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Investment investment)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Investments SET UserId = @User Id, InvestmentName = @InvestmentName, Amount = @Amount, InvestmentDate = @InvestmentDate, CurrentValue = @CurrentValue, UpdatedAt = GETDATE() WHERE InvestmentId = @InvestmentId", connection);
command.Parameters.AddWithValue("@InvestmentId", investment.InvestmentId);
command.Parameters.AddWithValue("@User Id", investment.UserId);
command.Parameters.AddWithValue("@InvestmentName", investment.InvestmentName);
command.Parameters.AddWithValue("@Amount", investment.Amount);
command.Parameters.AddWithValue("@InvestmentDate", investment.InvestmentDate);
command.Parameters.AddWithValue("@CurrentValue", investment.CurrentValue);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Investments WHERE InvestmentId = @InvestmentId", connection);
command.Parameters.AddWithValue("@InvestmentId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
NotificationRepository
public class NotificationRepository : IRepository<Notification>
{
private readonly string _connectionString;
public NotificationRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Notification> GetAll()
{
var notifications = new List<Notification>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Notifications", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
notifications.Add(new Notification
{
NotificationId = (int)reader["NotificationId"],
UserId = (int)reader["User Id"],
Message = reader["Message"].ToString(),
IsRead = (bool)reader["IsRead"],
CreatedAt = (DateTime)reader["CreatedAt"]
});
}
}
}
return notifications;
}
public Notification GetById(int id)
{
Notification notification = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Notifications WHERE NotificationId = @NotificationId", connection);
command.Parameters.AddWithValue("@NotificationId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
notification = new Notification
{
NotificationId = (int)reader["NotificationId"],
UserId = (int)reader["User Id"],
Message = reader["Message"].ToString(),
IsRead = (bool)reader["IsRead"],
CreatedAt = (DateTime)reader["CreatedAt"]
};
}
}
}
return notification;
}
public void Add(Notification notification)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Notifications (User Id, Message, IsRead) VALUES (@User Id, @Message, @IsRead)", connection);
command.Parameters.AddWithValue("@User Id", notification.UserId);
command.Parameters.AddWithValue("@Message", notification.Message);
command.Parameters.AddWithValue("@IsRead", notification.IsRead);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Notification notification)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Notifications SET UserId = @User Id, Message = @Message, IsRead = @IsRead, CreatedAt = GETDATE() WHERE NotificationId = @NotificationId", connection);
command.Parameters.AddWithValue("@NotificationId", notification.NotificationId);
command.Parameters.AddWithValue("@User Id", notification.UserId);
command.Parameters.AddWithValue("@Message", notification.Message);
command.Parameters.AddWithValue("@IsRead", notification.IsRead);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Notifications WHERE NotificationId = @NotificationId", connection);
command.Parameters.AddWithValue("@NotificationId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
BackupRepository
public class BackupRepository : IRepository<Backup>
{
private readonly string _connectionString;
public BackupRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Backup> GetAll()
{
var backups = new List<Backup ());
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Backups", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
backups.Add(new Backup
{
BackupId = (int)reader["BackupId"],
UserId = (int)reader["User Id"],
BackupDate = (DateTime)reader["BackupDate"],
BackupFile = reader["BackupFile"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"]
});
}
}
}
return backups;
}
public Backup GetById(int id)
{
Backup backup = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Backups WHERE BackupId = @BackupId", connection);
command.Parameters.AddWithValue("@BackupId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
backup = new Backup
{
BackupId = (int)reader["BackupId"],
UserId = (int)reader["User Id"],
BackupDate = (DateTime)reader["BackupDate"],
BackupFile = reader["BackupFile"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"]
};
}
}
}
return backup;
}
public void Add(Backup backup)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Backups (User Id, BackupDate, BackupFile) VALUES (@User Id, @BackupDate, @BackupFile)", connection);
command.Parameters.AddWithValue("@User Id", backup.UserId);
command.Parameters.AddWithValue("@BackupDate", backup.BackupDate);
command.Parameters.AddWithValue("@BackupFile", backup.BackupFile);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Backup backup)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Backups SET UserId = @User Id, BackupDate = @BackupDate, BackupFile = @BackupFile, CreatedAt = GETDATE() WHERE BackupId = @BackupId", connection);
command.Parameters.AddWithValue("@BackupId", backup.BackupId);
command.Parameters.AddWithValue("@User Id", backup.UserId);
command.Parameters.AddWithValue("@BackupDate", backup.BackupDate);
command.Parameters.AddWithValue("@BackupFile", backup.BackupFile);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Backups WHERE BackupId = @BackupId", connection);
command.Parameters.AddWithValue("@BackupId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
FeedbackRepository
public class FeedbackRepository : IRepository<Feedback>
{
private readonly string _connectionString;
public FeedbackRepository(string connectionString)
{
_connectionString = connectionString;
}
public IEnumerable<Feedback> GetAll()
{
var feedbacks = new List<Feedback>();
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Feedback", connection);
connection.Open();
using (var reader = command.ExecuteReader())
{
while (reader.Read())
{
feedbacks.Add(new Feedback
{
FeedbackId = (int)reader["FeedbackId"],
UserId = (int)reader["User Id"],
FeedbackContent = reader["FeedbackContent"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"]
});
}
}
}
return feedbacks;
}
public Feedback GetById(int id)
{
Feedback feedback = null;
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("SELECT * FROM Feedback WHERE FeedbackId = @FeedbackId", connection);
command.Parameters.AddWithValue("@FeedbackId", id);
connection.Open();
using (var reader = command.ExecuteReader())
{
if (reader.Read())
{
feedback = new Feedback
{
FeedbackId = (int)reader["FeedbackId"],
UserId = (int)reader["User Id"],
FeedbackContent = reader["FeedbackContent"].ToString(),
CreatedAt = (DateTime)reader["CreatedAt"]
};
}
}
}
return feedback;
}
public void Add(Feedback feedback)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("INSERT INTO Feedback (User Id, FeedbackContent) VALUES (@User Id, @FeedbackContent)", connection);
command.Parameters.AddWithValue("@User Id", feedback.UserId);
command.Parameters.AddWithValue("@FeedbackContent", feedback.FeedbackContent);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Update(Feedback feedback)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("UPDATE Feedback SET UserId = @User Id, FeedbackContent = @FeedbackContent, CreatedAt = GETDATE() WHERE FeedbackId = @FeedbackId", connection);
command.Parameters.AddWithValue("@FeedbackId", feedback.FeedbackId);
command.Parameters.AddWithValue("@User Id", feedback.UserId);
command.Parameters.AddWithValue("@FeedbackContent", feedback.FeedbackContent);
connection.Open();
command.ExecuteNonQuery();
}
}
public void Delete(int id)
{
using (var connection = new SqlConnection(_connectionString))
{
var command = new SqlCommand("DELETE FROM Feedback WHERE FeedbackId = @FeedbackId", connection);
command.Parameters.AddWithValue("@FeedbackId", id);
connection.Open();
command.ExecuteNonQuery();
}
}
}
This completes the repository implementations for all the models defined in your SQL Server schema
Each repository follows the same pattern, ensuring consistency and maintainability in your ASP.NET application.
To create controllers for each repository in an ASP.NET MVC application
We will follow the standard pattern of creating a controller for each model. Each controller will handle HTTP requests and interact with the corresponding repository to perform CRUD operations. Below are the example controllers for each of the models: User, Role, Income, Expense, Budget, Report, Goal, Investment, Notification, Backup, and Feedback.
Base Controller
First, let's create a base controller that can be inherited by all other controllers. This will help in managing common functionalities like dependency injection for the repositories.
using Microsoft.AspNetCore.Mvc;
public class BaseController : Controller
{
protected readonly string _connectionString;
public BaseController(string connectionString)
{
_connectionString = connectionString;
}
}
UserController
public class UserController : BaseController
{
private readonly UserRepository _userRepository;
public UserController(string connectionString) : base(connectionString)
{
_userRepository = new UserRepository(connectionString);
}
public IActionResult Index()
{
var users = _userRepository.GetAll();
return View(users);
}
public IActionResult Details(int id)
{
var user = _userRepository.GetById(id);
return View(user);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(User user)
{
if (ModelState.IsValid)
{
_userRepository.Add(user);
return RedirectToAction(nameof(Index));
}
return View(user);
}
public IActionResult Edit(int id)
{
var user = _userRepository.GetById(id);
return View(user);
}
[HttpPost]
public IActionResult Edit(User user)
{
if (ModelState.IsValid)
{
_userRepository.Update(user);
return RedirectToAction(nameof(Index));
}
return View(user);
}
public IActionResult Delete(int id)
{
var user = _userRepository.GetById(id);
return View(user);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_userRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
RoleController
public class RoleController : BaseController
{
private readonly RoleRepository _roleRepository;
public RoleController(string connectionString) : base(connectionString)
{
_roleRepository = new RoleRepository(connectionString);
}
public IActionResult Index()
{
var roles = _roleRepository.GetAll();
return View(roles);
}
public IActionResult Details(int id)
{
var role = _roleRepository.GetById(id);
return View(role);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Role role)
{
if (ModelState.IsValid)
{
_roleRepository.Add(role);
return RedirectToAction(nameof(Index));
}
return View(role);
}
public IActionResult Edit(int id)
{
var role = _roleRepository.GetById(id);
return View(role);
}
[HttpPost]
public IActionResult Edit(Role role)
{
if (ModelState.IsValid)
{
_roleRepository.Update(role);
return RedirectToAction(nameof(Index));
}
return View(role);
}
public IActionResult Delete(int id)
{
var role = _roleRepository.GetById(id);
return View(role);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_roleRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
IncomeController
public class IncomeController : BaseController
{
private readonly IncomeRepository _incomeRepository;
public IncomeController(string connectionString) : base(connectionString)
{
_incomeRepository = new IncomeRepository(connectionString);
}
public IActionResult Index()
{
var incomes = _incomeRepository.GetAll();
return View(incomes);
}
public IActionResult Details(int id)
{
var income = _incomeRepository.GetById(id);
return View(income);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Income income)
{
if (ModelState.IsValid)
{
_incomeRepository.Add(income);
return RedirectToAction(nameof(Index));
}
return View(income);
}
public IActionResult Edit(int id)
{
var income = _incomeRepository.GetById(id);
return View(income);
}
[HttpPost]
public IActionResult Edit(Income income)
{
if (ModelState.IsValid)
{
_incomeRepository.Update(income);
return RedirectToAction(nameof(Index));
}
return View(income);
}
public IActionResult Delete(int id)
{
var income = _incomeRepository.GetById(id);
return View(income);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_incomeRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
ExpenseController
public class ExpenseController : BaseController
{
private readonly ExpenseRepository _expenseRepository;
public ExpenseController(string connectionString) : base(connectionString)
{
_expenseRepository = new ExpenseRepository(connectionString);
}
public IActionResult Index()
{
var expenses = _expenseRepository.GetAll();
return View(expenses);
}
public IActionResult Details(int id)
{
var expense = _expenseRepository.GetById(id);
return View(expense);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Expense expense)
{
if (ModelState.IsValid)
{
_expenseRepository.Add(expense);
return RedirectToAction(nameof(Index));
}
return View(expense);
}
public IActionResult Edit(int id)
{
var expense = _expenseRepository.GetById(id);
return View(expense);
}
[HttpPost]
public IActionResult Edit(Expense expense)
{
if (ModelState.IsValid)
{
_expenseRepository.Update(expense);
return RedirectToAction(nameof(Index));
}
return View(expense);
}
public IActionResult Delete(int id)
{
var expense = _expenseRepository.GetById(id);
return View(expense);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_expenseRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
BudgetController
public class BudgetController : BaseController
{
private readonly BudgetRepository _budgetRepository;
public BudgetController(string connectionString) : base(connectionString)
{
_budgetRepository = new BudgetRepository(connectionString);
}
public IActionResult Index()
{
var budgets = _budgetRepository.GetAll();
return View(budgets);
}
public IActionResult Details(int id)
{
var budget = _budgetRepository.GetById(id);
return View(budget);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Budget budget)
{
if (ModelState.IsValid)
{
_budgetRepository.Add(budget);
return RedirectToAction(nameof(Index));
}
return View(budget);
}
public IActionResult Edit(int id)
{
var budget = _budgetRepository.GetById(id);
return View(budget);
}
[HttpPost]
public IActionResult Edit(Budget budget)
{
if (ModelState.IsValid)
{
_budgetRepository.Update(budget);
return RedirectToAction(nameof(Index));
}
return View(budget);
}
public IActionResult Delete(int id)
{
var budget = _budgetRepository.GetById(id);
return View(budget);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_budgetRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
ReportController
public class ReportController : BaseController
{
private readonly ReportRepository _reportRepository;
public ReportController(string connectionString) : base(connectionString)
{
_reportRepository = new ReportRepository(connectionString);
}
public IActionResult Index()
{
var reports = _reportRepository.GetAll();
return View(reports);
}
public IActionResult Details(int id)
{
var report = _reportRepository.GetById(id);
return View(report);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Report report)
{
if (ModelState.IsValid)
{
_reportRepository.Add(report);
return RedirectToAction(nameof(Index));
}
return View(report);
}
public IActionResult Edit(int id)
{
var report = _reportRepository.GetById(id);
return View(report);
}
[HttpPost]
public IActionResult Edit(Report report)
{
if (ModelState.IsValid)
{
_reportRepository.Update(report);
return RedirectToAction(nameof(Index));
}
return View(report);
}
public IActionResult Delete(int id)
{
var report = _reportRepository.GetById(id);
return View(report);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_reportRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
GoalController
public class GoalController : BaseController
{
private readonly GoalRepository _goalRepository;
public GoalController(string connectionString) : base(connectionString)
{
_goalRepository = new GoalRepository(connectionString);
}
public IActionResult Index()
{
var goals = _goalRepository.GetAll();
return View(goals);
}
public IActionResult Details(int id)
{
var goal = _goalRepository.GetById(id);
return View(goal);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Goal goal)
{
if (ModelState.IsValid)
{
_goalRepository.Add(goal);
return RedirectToAction(nameof(Index));
}
return View(goal);
}
public IActionResult Edit(int id)
{
var goal = _goalRepository.GetById (id);
return View(goal);
}
[HttpPost]
public IActionResult Edit(Goal goal)
{
if (ModelState.IsValid)
{
_goalRepository.Update(goal);
return RedirectToAction(nameof(Index));
}
return View(goal);
}
public IActionResult Delete(int id)
{
var goal = _goalRepository.GetById(id);
return View(goal);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_goalRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
InvestmentController
public class InvestmentController : BaseController
{
private readonly InvestmentRepository _investmentRepository;
public InvestmentController(string connectionString) : base(connectionString)
{
_investmentRepository = new InvestmentRepository(connectionString);
}
public IActionResult Index()
{
var investments = _investmentRepository.GetAll();
return View(investments);
}
public IActionResult Details(int id)
{
var investment = _investmentRepository.GetById(id);
return View(investment);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Investment investment)
{
if (ModelState.IsValid)
{
_investmentRepository.Add(investment);
return RedirectToAction(nameof(Index));
}
return View(investment);
}
public IActionResult Edit(int id)
{
var investment = _investmentRepository.GetById(id);
return View(investment);
}
[HttpPost]
public IActionResult Edit(Investment investment)
{
if (ModelState.IsValid)
{
_investmentRepository.Update(investment);
return RedirectToAction(nameof(Index));
}
return View(investment);
}
public IActionResult Delete(int id)
{
var investment = _investmentRepository.GetById(id);
return View(investment);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_investmentRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
NotificationController
public class NotificationController : BaseController
{
private readonly NotificationRepository _notificationRepository;
public NotificationController(string connectionString) : base(connectionString)
{
_notificationRepository = new NotificationRepository(connectionString);
}
public IActionResult Index()
{
var notifications = _notificationRepository.GetAll();
return View(notifications);
}
public IActionResult Details(int id)
{
var notification = _notificationRepository.GetById(id);
return View(notification);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Notification notification)
{
if (ModelState.IsValid)
{
_notificationRepository.Add(notification);
return RedirectToAction(nameof(Index));
}
return View(notification);
}
public IActionResult Edit(int id)
{
var notification = _notificationRepository.GetById(id);
return View(notification);
}
[HttpPost]
public IActionResult Edit(Notification notification)
{
if (ModelState.IsValid)
{
_notificationRepository.Update(notification);
return RedirectToAction(nameof(Index));
}
return View(notification);
}
public IActionResult Delete(int id)
{
var notification = _notificationRepository.GetById(id);
return View(notification);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_notificationRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
BackupController
public class BackupController : BaseController
{
private readonly BackupRepository _backupRepository;
public BackupController(string connectionString) : base(connectionString)
{
_backupRepository = new BackupRepository(connectionString);
}
public IActionResult Index()
{
var backups = _backupRepository.GetAll();
return View(backups);
}
public IActionResult Details(int id)
{
var backup = _backupRepository.GetById(id);
return View(backup);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Backup backup)
{
if (ModelState.IsValid)
{
_backupRepository.Add(backup);
return RedirectToAction(nameof(Index));
}
return View(backup);
}
public IActionResult Edit(int id)
{
var backup = _backupRepository.GetById(id);
return View(backup);
}
[HttpPost]
public IActionResult Edit(Backup backup)
{
if (ModelState.IsValid)
{
_backupRepository.Update( backup);
return RedirectToAction(nameof(Index));
}
return View(backup);
}
public IActionResult Delete(int id)
{
var backup = _backupRepository.GetById(id);
return View(backup);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_backupRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
FeedbackController
public class FeedbackController : BaseController
{
private readonly FeedbackRepository _feedbackRepository;
public FeedbackController(string connectionString) : base(connectionString)
{
_feedbackRepository = new FeedbackRepository(connectionString);
}
public IActionResult Index()
{
var feedbacks = _feedbackRepository.GetAll();
return View(feedbacks);
}
public IActionResult Details(int id)
{
var feedback = _feedbackRepository.GetById(id);
return View(feedback);
}
public IActionResult Create()
{
return View();
}
[HttpPost]
public IActionResult Create(Feedback feedback)
{
if (ModelState.IsValid)
{
_feedbackRepository.Add(feedback);
return RedirectToAction(nameof(Index));
}
return View(feedback);
}
public IActionResult Edit(int id)
{
var feedback = _feedbackRepository.GetById(id);
return View(feedback);
}
[HttpPost]
public IActionResult Edit(Feedback feedback)
{
if (ModelState.IsValid)
{
_feedbackRepository.Update(feedback);
return RedirectToAction(nameof(Index));
}
return View(feedback);
}
public IActionResult Delete(int id)
{
var feedback = _feedbackRepository.GetById(id);
return View(feedback);
}
[HttpPost, ActionName("Delete")]
public IActionResult DeleteConfirmed(int id)
{
_feedbackRepository.Delete(id);
return RedirectToAction(nameof(Index));
}
}
This completes the controller implementations for all the models defined in your application
Each controller follows the same pattern, ensuring consistency and maintainability in your ASP.NET MVC application. You can now create views for each action method to display and manage the data effectively.
Creating views for each controller in an ASP.NET MVC application
Involves creating Razor view files that correspond to the actions in the controllers. Below, I will provide a basic structure for the views for each controller: User, Role, Income, Expense, Budget, Report, Goal, Investment, Notification, Backup, and Feedback.
Folder Structure
Assuming you have a standard ASP.NET MVC project structure, the views for each controller will be placed in their respective folders under the Views directory:
/Views
/User
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Role
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Income
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Expense
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Budget
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Report
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Goal
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Investment
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Notification
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Backup
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
/Feedback
Create.cshtml
Delete.cshtml
Details.cshtml
Edit.cshtml
Index.cshtml
Example Views
Below are example Razor views for each action in the controllers. You can customize these views according to your application's design and requirements.
User Views
Index.cshtml
@model IEnumerable<User>
<h2>Users</h2>
<a href='@Url.Action("Create")'>Create New User</a>
<table>
<thead>
<tr>
<th>Username</th>
<th>Email</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var user in Model)
{
<tr>
<td>@user.Username</td>
<td>@user.Email</td>
<td>
<a href='@Url.Action("Details", new { id = user.UserId })">Details</a> |
<a href='@Url.Action("Edit", new { id = user.UserId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = user.UserId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model User
<h2>Create User</h2>
<form asp-action="Create">
<div>
<label>Username</label>
<input asp-for="Username" />
</div>
<div>
<label>Password</label>
<input asp-for="PasswordHash" type="password" />
</div>
<div>
<label>Email</label>
<input asp-for="Email" />
</div>
<div>
<label>First Name</label>
<input asp-for="FirstName" />
</div>
<div>
<label>Last Name</label>
<input asp-for="LastName" />
</div>
<div>
<label>Role ID</label>
<input asp-for="RoleId" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model User
<h2>Edit User</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="User Id" />
<div>
<label>Username</label>
<input asp-for="Username" />
</div>
<div>
<label>Password</label>
<input asp-for="PasswordHash" type="password" />
</div>
<div>
<label>Email</label>
<input asp-for="Email" />
</div>
<div>
<label>First Name</label>
<input asp-for="FirstName" />
</div>
<div>
<label>Last Name</label>
<input asp-for="LastName" />
</div>
<div>
<label>Role ID</label>
<input asp-for="RoleId" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model User
<h2>User Details</h2>
<div>
<h4>@Model.Username</h4>
<p>Email: @Model.Email</p>
<p>First Name: @Model.FirstName</p>
<p>Last Name: @Model.LastName</p>
<p>Role ID: @Model.RoleId</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.UserId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model User
<h2>Delete User</h2>
<div>
<h4>Are you sure you want to delete this user?</h4>
<p>@Model.Username</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for=" User Id" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Role Views
Index.cshtml
@model IEnumerable<Role>
<h2>Roles</h2>
<a href='@Url.Action("Create")'>Create New Role</a>
<table>
<thead>
<tr>
<th>Role Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var role in Model)
{
<tr>
<td>@role.Name</td>
<td>
<a href='@Url.Action("Details", new { id = role.RoleId })">Details</a> |
<a href='@Url.Action("Edit", new { id = role.RoleId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = role.RoleId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model Role
<h2>Create Role</h2>
<form asp-action="Create">
<div>
<label>Role Name</label>
<input asp-for="Name" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Role
<h2>Edit Role</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="RoleId" />
<div>
<label>Role Name</label>
<input asp-for="Name" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Role
<h2>Role Details</h2>
<div>
<h4>@Model.Name</h4>
</div>
<a href='@Url.Action("Edit", new { id = Model.RoleId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Role
<h2>Delete Role</h2>
<div>
<h4>Are you sure you want to delete this role?</h4>
<p>@Model.Name</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="RoleId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Income Views
Index.cshtml
@model IEnumerable<Income>
<h2>Incomes</h2>
<a href='@Url.Action("Create")'>Create New Income</a>
<table>
<thead>
<tr>
<th>Source</th>
<th>Amount</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var income in Model)
{
<tr>
<td>@income.Source</td>
<td>@income.Amount</td>
<td>@income.Date.ToShortDateString()</td>
<td>
<a href='@Url.Action("Details", new { id = income.IncomeId })">Details</a> |
<a href='@Url.Action("Edit", new { id = income.IncomeId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = income.IncomeId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model Income
<h2>Create Income</h2>
<form asp-action="Create">
<div>
<label>Source</label>
<input asp-for="Source" />
</div>
<div>
<label>Amount</label>
<input asp-for="Amount" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Income
<h2>Edit Income</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="IncomeId" />
<div>
<label>Source</label>
<input asp-for="Source" />
</div>
<div>
<label>Amount</label>
<input asp-for="Amount" />
</div>
<div>
<label>Date</label>
<input asp-for=" Date" type="date" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Income
<h2>Income Details</h2>
<div>
<h4>Source: @Model.Source</h4>
<p>Amount: @Model.Amount</p>
<p>Date: @Model.Date.ToShortDateString()</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.IncomeId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Income
<h2>Delete Income</h2>
<div>
<h4>Are you sure you want to delete this income?</h4>
<p>Source: @Model.Source</p>
<p>Amount: @Model.Amount</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="IncomeId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Expense Views
Index.cshtml
@model IEnumerable<Expense>
<h2>Expenses</h2>
<a href='@Url.Action("Create")'>Create New Expense</a>
<table>
<thead>
<tr>
<th>Description</th>
<th>Amount</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var expense in Model)
{
<tr>
<td>@expense.Description</td>
<td>@expense.Amount</td>
<td>@expense.Date.ToShortDateString()</td>
<td>
<a href='@Url.Action("Details", new { id = expense.ExpenseId })">Details</a> |
<a href='@Url.Action("Edit", new { id = expense.ExpenseId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = expense.ExpenseId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model Expense
<h2>Create Expense</h2>
<form asp-action="Create">
<div>
<label>Description</label>
<input asp-for="Description" />
</div>
<div>
<label>Amount</label>
<input asp-for="Amount" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Expense
<h2>Edit Expense</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="ExpenseId" />
<div>
<label>Description</label>
<input asp-for="Description" />
</div>
<div>
<label>Amount</label>
<input asp-for="Amount" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Expense
<h2>Expense Details</h2>
<div>
<h4>Description: @Model.Description</h4>
<p>Amount: @Model.Amount</p>
<p>Date: @Model.Date.ToShortDateString()</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.ExpenseId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Expense
<h2>Delete Expense</h2>
<div>
<h4>Are you sure you want to delete this expense?</h4>
<p>Description: @Model.Description</p>
<p>Amount: @Model.Amount</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="ExpenseId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Budget Views
Index.cshtml
@model IEnumerable<Budget>
<h2>Budgets</h2>
<a href='@Url.Action("Create")'>Create New Budget</a>
<table>
<thead>
<tr>
<th>Name</th>
< th>Amount</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var budget in Model)
{
<tr>
<td>@budget.Name</td>
<td>@budget.Amount</td>
<td>
<a href='@Url.Action("Details", new { id = budget.BudgetId })">Details</a> |
<a href='@Url.Action("Edit", new { id = budget.BudgetId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = budget.BudgetId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model Budget
<h2>Create Budget</h2>
<form asp-action="Create">
<div>
<label>Name</label>
<input asp-for="Name" />
</div>
<div>
<label>Amount</label>
<input asp-for="Amount" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Budget
<h2>Edit Budget</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="BudgetId" />
<div>
<label>Name</label>
<input asp-for="Name" />
</div>
<div>
<label>Amount</label>
<input asp-for="Amount" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Budget
<h2>Budget Details</h2>
<div>
<h4>Name: @Model.Name</h4>
<p>Amount: @Model.Amount</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.BudgetId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Budget
<h2>Delete Budget</h2>
<div>
<h4>Are you sure you want to delete this budget?</h4>
<p>Name: @Model.Name</p>
<p>Amount: @Model.Amount</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="BudgetId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Report Views
Index.cshtml
@model IEnumerable<Report>
<h2>Reports</h2>
<a href='@Url.Action("Create")'>Create New Report</a>
<table>
<thead>
<tr>
<th>Title</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var report in Model)
{
<tr>
<td>@report.Title</td>
<td>@report.Date.ToShortDateString()</td>
<td>
<a href='@Url.Action("Details", new { id = report.ReportId })">Details</a> |
<a href='@Url.Action("Edit", new { id = report.ReportId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = report.ReportId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model Report
<h2>Create Report</h2>
<form asp-action="Create">
<div>
<label>Title</label>
<input asp-for="Title" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Report
<h2>Edit Report</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="ReportId" />
<div>
<label>Title</label>
<input asp-for="Title" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Report
<h2>Report Details</h2>
<div>
<h4>Title: @Model.Title</h4>
<p>Date: @ Model.Date.ToShortDateString()</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.ReportId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Report
<h2>Delete Report</h2>
<div>
<h4>Are you sure you want to delete this report?</h4>
<p>Title: @Model.Title</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="ReportId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Goal Views
Index.cshtml
@model IEnumerable<Goal>
<h2>Goals</h2>
<a href='@Url.Action("Create")'>Create New Goal</a>
<table>
<thead>
<tr>
<th>Goal Name</th>
<th>Target Amount</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var goal in Model)
{
<tr>
<td>@goal.Name</td>
<td>@goal.TargetAmount</td>
<td>
<a href='@Url.Action("Details", new { id = goal.GoalId })">Details</a> |
<a href='@Url.Action("Edit", new { id = goal.GoalId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = goal.GoalId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model Goal
<h2>Create Goal</h2>
<form asp-action="Create">
<div>
<label>Name</label>
<input asp-for="Name" />
</div>
<div>
<label>Target Amount</label>
<input asp-for="TargetAmount" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Goal
<h2>Edit Goal</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="GoalId" />
<div>
<label>Name</label>
<input asp-for="Name" />
</div>
<div>
<label>Target Amount</label>
<input asp-for="TargetAmount" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Goal
<h2>Goal Details</h2>
<div>
<h4>Name: @Model.Name</h4>
<p>Target Amount: @Model.TargetAmount</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.GoalId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Goal
<h2>Delete Goal</h2>
<div>
<h4>Are you sure you want to delete this goal?</h4>
<p>Name: @Model.Name</p>
<p>Target Amount: @Model.TargetAmount</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="GoalId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Investment Views
Index.cshtml
@model IEnumerable<Investment>
<h2>Investments</h2>
<a href='@Url.Action("Create")'>Create New Investment</a>
<table>
<thead>
<tr>
<th>Investment Name</th>
<th>Amount</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var investment in Model)
{
<tr>
<td>@investment.Name</td>
<td>@investment.Amount</td>
<td>@investment.Date.ToShortDateString()</td>
<td>
<a href='@Url.Action("Details", new { id = investment.InvestmentId })">Details</a> |
<a href='@Url.Action("Edit", new { id = investment.InvestmentId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = investment.InvestmentId })'>Delete</a>
</td>
</tr>
}
</ <tbody>
</table>
Create.cshtml
@model Investment
<h2>Create Investment</h2>
<form asp-action="Create">
<div>
<label>Name</label>
<input asp-for="Name" />
</div>
<div>
<label>Amount</label>
<input asp-for="Amount" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Investment
<h2>Edit Investment</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="InvestmentId" />
<div>
<label>Name</label>
<input asp-for="Name" />
</div>
<div>
<label>Amount</label>
<input asp-for="Amount" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Investment
<h2>Investment Details</h2>
<div>
<h4>Name: @Model.Name</h4>
<p>Amount: @Model.Amount</p>
<p>Date: @Model.Date.ToShortDateString()</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.InvestmentId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Investment
<h2>Delete Investment</h2>
<div>
<h4>Are you sure you want to delete this investment?</h4>
<p>Name: @Model.Name</p>
<p>Amount: @Model.Amount</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="InvestmentId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Notification Views
Index.cshtml
@model IEnumerable<Notification>
<h2>Notifications</h2>
<a href='@Url.Action("Create")'>Create New Notification</a>
<table>
<thead>
<tr>
<th>Message</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var notification in Model)
{
<tr>
<td>@notification.Message</td>
<td>@notification.Date.ToShortDateString()</td>
<td>
<a href='@Url.Action("Details", new { id = notification.NotificationId })">Details</a> |
<a href='@Url.Action("Edit", new { id = notification.NotificationId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = notification.NotificationId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model Notification
<h2>Create Notification</h2>
<form asp-action="Create">
<div>
<label>Message</label>
<input asp-for="Message" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Notification
<h2>Edit Notification</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="NotificationId" />
<div>
<label>Message</label>
<input asp-for="Message" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Notification
<h2>Notification Details</h2>
<div>
<h4>Message: @Model.Message</h4>
<p>Date: @Model.Date.ToShortDateString()</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.NotificationId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Notification
<h2>Delete Notification</h2>
<div>
<h4>Are you sure you want to delete this notification?</h4>
<p>Message: @Model.Message</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="NotificationId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Backup Views
Index.cshtml
@model IEnumerable<Backup>
<h2>Backups</h2>
<a href='@Url.Action("Create")'>Create New Backup</a>
<table>
<thead>
<tr>
<th>Backup Name</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var backup in Model)
{
<tr>
<td>@backup.Name</td>
<td>@backup.Date.ToShortDateString()</td>
<td>
<a href='@Url.Action("Details", new { id = backup.BackupId })">Details</a> |
<a href='@Url.Action("Edit", new { id = backup.BackupId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = backup.BackupId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model Backup
<h2>Create Backup</h2>
<form asp-action="Create">
<div>
<label>Name</label>
<input asp-for="Name" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Backup
<h2>Edit Backup</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="BackupId" />
<div>
<label>Name</label>
<input asp-for="Name" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Backup
<h2>Backup Details</h2>
<div>
<h4>Name: @Model.Name</h4>
<p>Date: @Model.Date.ToShortDateString()</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.BackupId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Backup
<h2>Delete Backup</h2>
<div>
<h4>Are you sure you want to delete this backup?</h4>
<p>Name: @Model.Name</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="BackupId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
Feedback Views
Index.cshtml
@model IEnumerable<Feedback>
<h2>Feedbacks</h2>
<a href='@Url.Action("Create")'>Create New Feedback</a>
<table>
<thead>
<tr>
<th>Message</th>
<th>Date</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
@foreach (var feedback in Model)
{
<tr>
<td>@feedback.Message</td>
<td>@feedback.Date.ToShortDateString()</td>
<td>
<a href='@Url.Action("Details", new { id = feedback.FeedbackId })">Details</a> |
<a href='@Url.Action("Edit", new { id = feedback.FeedbackId })'>Edit</a> |
<a href='@Url.Action("Delete", new { id = feedback.FeedbackId })'>Delete</a>
</td>
</tr>
}
</tbody>
</table>
Create.cshtml
@model Feedback
<h2>Create Feedback</h2>
<form asp-action="Create">
<div>
<label>Message</label>
<input asp-for="Message" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Create</button>
</form>
Edit.cshtml
@model Feedback
<h2>Edit Feedback</h2>
<form asp-action="Edit">
<input type="hidden" asp-for="FeedbackId" />
<div>
<label>Message</label>
<input asp-for="Message" />
</div>
<div>
<label>Date</label>
<input asp-for="Date" type="date" />
</div>
<button type="submit">Save</button>
</form>
Details.cshtml
@model Feedback
<h2>Feedback Details</h2>
<div>
<h4>Message: @Model.Message</h4>
<p>Date: @Model.Date.ToShortDateString()</p>
</div>
<a href='@Url.Action("Edit", new { id = Model.FeedbackId })'>Edit</a> |
<a href='@Url.Action("Index")">Back to List</a>
Delete.cshtml
@model Feedback
<h2>Delete Feedback</h2>
<div>
<h4>Are you sure you want to delete this feedback?</h4>
<p>Message: @Model.Message</p>
</div>
<form asp-action="DeleteConfirmed">
<input type="hidden" asp-for="FeedbackId" />
<button type="submit">Delete</button>
</form>
<a href='@Url.Action("Index")'>Cancel</a>
This completes the view files for all the controllers in your ASP.NET MVC application
Each view corresponds to the actions defined in the controllers, allowing for the management of the respective models. You can further customize the layout and styling of these views to fit your application's design requirements.
Creating a dashboard page in an ASP.NET MVC application
Involves aggregating data from various models and presenting it in a user-friendly format. Below, I will outline how to create a dashboard page that consolidates data related to users, roles, incomes, expenses, budgets, reports, goals, investments, notifications, backups, and feedback.
Step 1: Create a Dashboard ViewModel
First, we need to create a ViewModel that will hold the consolidated data for the dashboard.
public class DashboardViewModel
{
public int TotalUsers { get; set; }
public int TotalRoles { get; set; }
public decimal TotalIncome { get; set; }
public decimal TotalExpenses { get; set; }
public decimal TotalBudget { get; set; }
public int TotalReports { get; set; }
public int TotalGoals { get; set; }
public decimal TotalInvestments { get; set; }
public int TotalNotifications { get; set; }
public int TotalBackups { get; set; }
public int TotalFeedbacks { get; set; }
}
Step 2: Create a Dashboard Controller
Next, we will create a controller that will handle the logic for the dashboard.
public class DashboardController : BaseController
{
private readonly UserRepository _userRepository;
private readonly RoleRepository _roleRepository;
private readonly IncomeRepository _incomeRepository;
private readonly ExpenseRepository _expenseRepository;
private readonly BudgetRepository _budgetRepository;
private readonly ReportRepository _reportRepository;
private readonly GoalRepository _goalRepository;
private readonly InvestmentRepository _investmentRepository;
private readonly NotificationRepository _notificationRepository;
private readonly BackupRepository _backupRepository;
private readonly FeedbackRepository _feedbackRepository;
public DashboardController(string connectionString) : base(connectionString)
{
_userRepository = new UserRepository(connectionString);
_roleRepository = new RoleRepository(connectionString);
_incomeRepository = new IncomeRepository(connectionString);
_expenseRepository = new ExpenseRepository(connectionString);
_budgetRepository = new BudgetRepository(connectionString);
_reportRepository = new ReportRepository(connectionString);
_goalRepository = new GoalRepository(connectionString);
_investmentRepository = new InvestmentRepository(connectionString);
_notificationRepository = new NotificationRepository(connectionString);
_backupRepository = new BackupRepository(connectionString);
_feedbackRepository = new FeedbackRepository(connectionString);
}
public IActionResult Index()
{
var model = new DashboardViewModel
{
TotalUsers = _userRepository.GetAll().Count(),
TotalRoles = _roleRepository.GetAll().Count(),
TotalIncome = _incomeRepository.GetAll().Sum(i => i.Amount),
TotalExpenses = _expenseRepository.GetAll().Sum(e => e.Amount),
TotalBudget = _budgetRepository.GetAll().Sum(b => b.Amount),
TotalReports = _reportRepository.GetAll().Count(),
TotalGoals = _goalRepository.GetAll().Count(),
TotalInvestments = _investmentRepository.GetAll().Sum(i => i.Amount),
TotalNotifications = _notificationRepository.GetAll().Count(),
TotalBackups = _backupRepository.GetAll().Count(),
TotalFeedbacks = _feedbackRepository.GetAll().Count()
};
return View(model);
}
}
Step 3: Create the Dashboard View
Now, we will create the view for the dashboard. This view will display the consolidated data in a user-friendly format.
Views/Dashboard/Index.cshtml
@model DashboardViewModel
<h2>Dashboard</h2>
<div class="dashboard">
<div class="card">
<h3>Total Users</h3>
<p>@Model.TotalUsers</p>
</div>
<div class="card">
<h3>Total Roles</h3>
<p>@Model.TotalRoles</p>
</div>
<div class="card">
<h3>Total Income</h3>
<p>@Model.TotalIncome.ToString("C")</p>
</div>
<div class="card">
<h3>Total Expenses</h3>
<p>@Model.TotalExpenses.ToString("C")</p>
</div>
<div class="card">
<h3>Total Budget</h3>
<p>@Model.TotalBudget.ToString("C")</p>
</div>
<div class="card">
<h3>Total Reports</h3>
<p>@Model.TotalReports</p>
</div>
<div class="card">
<h3>Total Goals</h3>
<p>@Model.TotalGoals</p>
</div>
<div class="card">
<h3>Total Investments</h3>
<p>@Model.TotalInvestments.ToString("C")</p>
</div>
<div class="card">
<h3>Total Notifications</h3>
<p>@Model.TotalNotifications</p>
</div>
<div class="card">
<h3>Total Backups</h3>
<p>@Model.TotalBackups</p>
</div>
<div class="card">
<h3>Total Feedbacks</h3>
<p>@Model.TotalFeedbacks</p>
</div>
</div>
<style>
.dashboard {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
gap: 20px;
}
.card {
border: 1px solid #ccc;
padding: 20px;
border-radius: 5px;
text-align: center;
}
</style>
Step 4: Update the Navigation
To access the dashboard, you may want to add a link to it in your main layout or navigation menu.
_Layout.cshtml (or wherever your navigation is defined)
<ul>
<li><a href='@Url.Action("Index", "Dashboard")">Dashboard</a></li>
<li><a href='@Url.Action("Index", "User ")">Users</a></li>
<li><a href='@Url.Action("Index", "Role")">Roles</a></li>
<li><a href='@Url.Action("Index", "Income")">Incomes</a></li>
<li><a href='@Url.Action("Index", "Expense")">Expenses</a></li>
<li><a href='@Url.Action("Index", "Budget")">Budgets</a></li>
<li><a href='@Url.Action("Index", "Report")">Reports</a></li>
<li><a href='@Url.Action("Index", "Goal")">Goals</a></li>
<li><a href='@Url.Action("Index", "Investment")">Investments</a></li>
<li><a href='@Url.Action("Index", "Notification")">Notifications</a></li>
<li><a href='@Url.Action("Index", "Backup")">Backups</a></li>
<li><a href='@Url.Action("Index", "Feedback")">Feedbacks</a></li>
</ul>