Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions PhoneBook.davetn657/Controllers/OptionUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System.ComponentModel;

namespace PhoneBook.davetn657.Controllers;
public class OptionUtils
{

public string GetStringValue(Enum value)
{
var info = value.GetType().GetField(value.ToString());
var attributes = (DescriptionAttribute[])info.GetCustomAttributes(typeof(DescriptionAttribute), false);

if(attributes.Length > 0)
{
return attributes[0].Description;
}
return string.Empty;
}

public List<string> GetAllStringValues(Type enumType)
{
var enumValues = enumType.GetEnumValues();
var allValues = new List<string>();

foreach (var value in enumValues)
{
allValues.Add(GetStringValue((Enum)value));
}

return allValues;
}

public Enum GetEnumValue(string description, Type enumType)
{
var enumValues = enumType.GetEnumValues();

foreach(var value in enumValues)
{
if (GetStringValue((Enum)value).Equals(description))
{
return (Enum) value;
}
}
return null;
}
}
29 changes: 29 additions & 0 deletions PhoneBook.davetn657/Controllers/Validation.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System.Net.Mail;
using System.Text.RegularExpressions;

namespace PhoneBook.davetn657.Controllers;
public class Validation
{

public bool ValidatePhoneNumber(string number)
{
var phonePattern = @"^\d{3}-\d{3}-\d{4}$";

return !string.IsNullOrEmpty(number) && Regex.IsMatch(number, phonePattern);
}

public bool ValidateEmail(string email)
{
if(string.IsNullOrEmpty(email)) return false;
try
{
var address = new MailAddress(email);

return MailAddress.TryCreate(email, out address);
}
catch(FormatException)
{
return false;
}
}
}
37 changes: 37 additions & 0 deletions PhoneBook.davetn657/Data/PhoneBookContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System.ComponentModel.DataAnnotations;

namespace PhoneBook.davetn657.Data;

public class PhoneBookContext : DbContext
{
private IConfiguration configuration;

public DbSet<PhoneProperties> PhoneProperties { get; set; }
public string? DbPath { get; set; }

public PhoneBookContext()
{
configuration = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json")
.Build();

DbPath = configuration["Database"];
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
=> optionsBuilder.UseSqlite($"Data Source={DbPath}");
}

public class PhoneProperties
{
[Key]
public int Id { get; set; }
[Required]
public string Name { get; set; } = string.Empty;
public string PhoneNumber { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public string Category { get; set; } = string.Empty;
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions PhoneBook.davetn657/Migrations/20260410181957_MovedDBContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace PhoneBook.davetn657.Migrations
{
/// <inheritdoc />
public partial class MovedDBContext : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "PhoneProperties",
columns: table => new
{
Id = table.Column<int>(type: "INTEGER", nullable: false)
.Annotation("Sqlite:Autoincrement", true),
Name = table.Column<string>(type: "TEXT", nullable: false),
PhoneNumber = table.Column<string>(type: "TEXT", nullable: false),
Email = table.Column<string>(type: "TEXT", nullable: false),
Category = table.Column<string>(type: "TEXT", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_PhoneProperties", x => x.Id);
});
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "PhoneProperties");
}
}
}
48 changes: 48 additions & 0 deletions PhoneBook.davetn657/Migrations/PhoneBookContextModelSnapshot.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
// <auto-generated />
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
using PhoneBook.davetn657.Data;

#nullable disable

namespace PhoneBook.davetn657.Migrations
{
[DbContext(typeof(PhoneBookContext))]
partial class PhoneBookContextModelSnapshot : ModelSnapshot
{
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder.HasAnnotation("ProductVersion", "8.0.8");

modelBuilder.Entity("PhoneBook.davetn657.Data.PhoneProperties", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("INTEGER");

b.Property<string>("Category")
.IsRequired()
.HasColumnType("TEXT");

b.Property<string>("Email")
.IsRequired()
.HasColumnType("TEXT");

b.Property<string>("Name")
.IsRequired()
.HasColumnType("TEXT");

b.Property<string>("PhoneNumber")
.IsRequired()
.HasColumnType("TEXT");

b.HasKey("Id");

b.ToTable("PhoneProperties");
});
#pragma warning restore 612, 618
}
}
}
32 changes: 32 additions & 0 deletions PhoneBook.davetn657/Models/Options.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System.ComponentModel;

public enum MainMenuOptions
{
[Description("Exit")]Exit,
[Description("Add Contact")]AddContacts
}

public enum AddNewContactOptions
{
[Description("Return")]Return,
[Description("Phone Number")]PhoneNumber,
[Description("Email")]Email,
[Description("Both")]Both
}

public enum EditContactOptions
{
[Description("Return")] Return,
[Description("Send Email")] SendEmail,
[Description("Add to Group")] AddGroup,
[Description("Change Phone Number")] ChangePhone,
[Description("Change Email")] ChangeEmail,
[Description("Change Name")] ChangeName,
[Description("Delete")] Delete
}

public enum AddCategoryOptions
{
[Description("Return")] Return,
[Description("Create new category")] CreateCategory
}
26 changes: 26 additions & 0 deletions PhoneBook.davetn657/PhoneBook.davetn657.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="8.0.8">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" />
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.3" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="10.0.3" />
<PackageReference Include="Spectre.Console" Version="0.54.0" />
</ItemGroup>

<ItemGroup>
<Folder Include="Migrations\" />
<Folder Include="READMEImgs\" />
</ItemGroup>

</Project>
Binary file added PhoneBook.davetn657/PhoneBook.davetn657.db
Binary file not shown.
31 changes: 31 additions & 0 deletions PhoneBook.davetn657/PhoneBook.davetn657.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 17
VisualStudioVersion = 17.14.37012.4
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PhoneBook.davetn657", "PhoneBook.davetn657.csproj", "{1D67DE61-5075-4E08-8DA0-E001378CC4F4}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Phonebook.davetn657.Tests", "..\Phonebook.davetn657.Tests\Phonebook.davetn657.Tests.csproj", "{7066BA6E-2FE9-4CDA-9086-3D4963EF6D27}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{1D67DE61-5075-4E08-8DA0-E001378CC4F4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1D67DE61-5075-4E08-8DA0-E001378CC4F4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1D67DE61-5075-4E08-8DA0-E001378CC4F4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1D67DE61-5075-4E08-8DA0-E001378CC4F4}.Release|Any CPU.Build.0 = Release|Any CPU
{7066BA6E-2FE9-4CDA-9086-3D4963EF6D27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7066BA6E-2FE9-4CDA-9086-3D4963EF6D27}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7066BA6E-2FE9-4CDA-9086-3D4963EF6D27}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7066BA6E-2FE9-4CDA-9086-3D4963EF6D27}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {DEC8DAEA-B3DF-4F51-BCDB-53608537748E}
EndGlobalSection
EndGlobal
19 changes: 19 additions & 0 deletions PhoneBook.davetn657/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using PhoneBook.davetn657.Controllers;
using PhoneBook.davetn657.Data;
using PhoneBook.davetn657.Views;

namespace PhoneBook.davetn657
{
internal class Program
{
static void Main(string[] args)
{
var phoneBook = new PhoneBookContext();
var optionUtils = new OptionUtils();
var validation = new Validation();
var uI = new UserInterface(phoneBook, optionUtils, validation);

uI.Start();
}
}
}
8 changes: 8 additions & 0 deletions PhoneBook.davetn657/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"profiles": {
"PhoneBook.davetn657": {
"commandName": "Project",
"workingDirectory": "C:\\Users\\davei\\Documents\\.Coding\\.C#Academy\\.Backup\\Phonebook.Console.davetn657\\PhoneBook.davetn657"
}
}
}
Loading