Skip to content

Commit

Permalink
Add Message.DAL
Browse files Browse the repository at this point in the history
  • Loading branch information
atrakic committed Jun 8, 2024
1 parent ec1364e commit c19b490
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 1 deletion.
2 changes: 1 addition & 1 deletion config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ packageName: Echo.OpenAPI
useDateTimeForDate: true

# Deserialize array types to Collection<T> instead of List<T>. (Default: false)
# useCollection: true
useCollection: true
19 changes: 19 additions & 0 deletions src/Message.DAL/IMessageRepository.cs
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();
}
}
13 changes: 13 additions & 0 deletions src/Message.DAL/Message.DAL.csproj
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>
16 changes: 16 additions & 0 deletions src/Message.DAL/MessageDbContext.cs
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; }
}
}
62 changes: 62 additions & 0 deletions src/Message.DAL/MessageRepository.cs
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();
}
}
}
10 changes: 10 additions & 0 deletions src/Message.DAL/Model/MessageItem.cs
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; }
}
}

0 comments on commit c19b490

Please sign in to comment.