Skip to content
Open
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
3 changes: 3 additions & 0 deletions Phone Book/Phone Book.slnx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<Solution>
<Project Path="Phone Book/PhoneBook.csproj" />
</Solution>
58 changes: 58 additions & 0 deletions Phone Book/Phone Book/CategoryController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.EntityFrameworkCore;
using Phone_Book.Models;
using Spectre.Console;

namespace PhoneBook
{
internal class CategoryController
{
public void CreateCategory(Category userCategory)
{
using var db = new ContactContext();
db.Categories.Add(userCategory);

SaveChanges(db);
}

public List<Category> GetAllCategories()
{
using var db = new ContactContext();
List<Category> categories = db.Categories.ToList();
return categories;
}

public void UpdateCategory(Category userCategory)
{
using var db = new ContactContext();
db.Categories.Update(userCategory);

SaveChanges(db);
}

public void DeleteCategory(Category userCategory)
{
using var db = new ContactContext();
db.Categories.Remove(userCategory);

SaveChanges(db);
}

public void SaveChanges(ContactContext db)
{
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
AnsiConsole.MarkupLine("[Red]ERROR [/]Couldn't save changes");
Environment.Exit(0);
}
catch (DbUpdateException)
{
AnsiConsole.MarkupLine("[Red]ERROR [/]Couldn't save changes");
Environment.Exit(0);
}
}
}
}
30 changes: 30 additions & 0 deletions Phone Book/Phone Book/ContactContext.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using Microsoft.EntityFrameworkCore;
using Phone_Book.Models;
using PhoneBook.Models;

namespace PhoneBook
{
public class ContactContext : DbContext
{
public DbSet<Contact> Contacts { get; set; }
public DbSet<Category> Categories { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=localhost; Initial Catalog=PhoneBook; Integrated Security=True; TrustServerCertificate=True;");
}

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Contact>().HasOne(contact => contact.Category).WithMany(category => category.Contacts).HasForeignKey(contact => contact.CategoryId).OnDelete(DeleteBehavior.Cascade);

modelBuilder.Entity<Contact>().HasData(
new Contact { CategoryId = 2, Id = 1, PhoneNumber = "934568035", Email = "johnjohnson@gmail.com", ContactName = "John" },
new Contact { CategoryId = 1, Id = 2, PhoneNumber = "324532653", Email = "jankowalski@yahoo.com", ContactName = "Jan" },
new Contact { CategoryId = 1, Id = 3, PhoneNumber = "934568035", Email = "karen609@outlook.com", ContactName = "Karen" });
modelBuilder.Entity<Category>().HasData(
new Category { Id = 1, Name = "Friends" },
new Category { Id = 2, Name = "Family" });
}
}
}
58 changes: 58 additions & 0 deletions Phone Book/Phone Book/ContactController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using Microsoft.EntityFrameworkCore;
using PhoneBook.Models;
using Spectre.Console;

namespace PhoneBook
{
internal class ContactController
{
public void CreateContact(Contact userContact)
{
using var db = new ContactContext();
db.Contacts.Add(userContact);

SaveChanges(db);
}

public List<Contact> GetAllContacts()
{
using var db = new ContactContext();
List<Contact> contacts = db.Contacts.Include(contact => contact.Category).ToList();
return contacts;
}

public void UpdateContact(Contact userContact)
{
using var db = new ContactContext();
db.Contacts.Update(userContact);

SaveChanges(db);
}

public void DeleteContact(Contact userContact)
{
using var db = new ContactContext();
db.Contacts.Remove(userContact);

SaveChanges(db);
}

public void SaveChanges(ContactContext db)
{
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
AnsiConsole.MarkupLine("[Red]ERROR [/]Couldn't save changes");
Environment.Exit(0);
}
catch (DbUpdateException)
{
AnsiConsole.MarkupLine("[Red]ERROR [/]Couldn't save changes");
Environment.Exit(0);
}
}
}
}
Loading