-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
121 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Message.Model; | ||
|
||
namespace Message.DAL | ||
{ | ||
public interface IMessageRepository | ||
{ | ||
MessageItem? GetItem(int id); | ||
|
||
IEnumerable<MessageItem> GetItems(); | ||
|
||
void AddItem(MessageItem item); | ||
|
||
void UpdateItem(MessageItem item); | ||
|
||
void RemoveItem(MessageItem item); | ||
|
||
void SaveChanges(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="8.0.6" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
using Message.Model; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
|
||
namespace Message.DAL | ||
{ | ||
public class MessageDbContext : DbContext | ||
{ | ||
public MessageDbContext(DbContextOptions<MessageDbContext> options) : base(options) | ||
{ | ||
} | ||
|
||
public DbSet<MessageItem> Items { get; set; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
using Message.Model; | ||
|
||
using Microsoft.EntityFrameworkCore; | ||
using System; | ||
using System.Xml.Linq; | ||
|
||
namespace Message.DAL | ||
{ | ||
public class MessageRepository : IMessageRepository | ||
{ | ||
private readonly MessageDbContext context; | ||
public MessageRepository(MessageDbContext context) | ||
{ | ||
this.context = context; | ||
|
||
if (context.Items.Any()) | ||
{ | ||
return; | ||
} | ||
|
||
var messageItems = new MessageItem[] | ||
{ | ||
new MessageItem { Name = "Item1" }, | ||
new MessageItem { Name = "Item2" }, | ||
new MessageItem { Name = "Item3" }, | ||
}; | ||
|
||
context.Items.AddRange(messageItems); | ||
context.SaveChanges(); | ||
} | ||
|
||
public void AddItem(MessageItem item) | ||
{ | ||
context.Items.Add(item); | ||
} | ||
|
||
public void UpdateItem(MessageItem item) | ||
{ | ||
context.Items.Update(item); | ||
} | ||
|
||
public MessageItem? GetItem(int id) | ||
{ | ||
return context.Items.FirstOrDefault(x => x.Id.Equals(id)); | ||
} | ||
|
||
public IEnumerable<MessageItem> GetItems() | ||
{ | ||
return context.Items.ToList(); | ||
} | ||
|
||
public void RemoveItem(MessageItem item) | ||
{ | ||
context.Items.Remove(item); | ||
} | ||
|
||
public void SaveChanges() | ||
{ | ||
context.SaveChanges(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
using System.ComponentModel.DataAnnotations.Schema; | ||
|
||
namespace Message.Model | ||
{ | ||
public class MessageItem | ||
{ | ||
public int Id { get; private set; } | ||
public string? Name { get; set; } | ||
} | ||
} |