<file_sep>﻿using System;
using System.Collections.Generic;
using System.Text;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Interfaces;

namespace CleanArchitecture.Core.Services
{
    public class GuestbookService : IGuestbookService
    {
        private readonly IRepository<Guestbook> _guestbookRepository;
        private readonly IMessageSender _messageSender;

        public GuestbookService(IRepository<Guestbook> guestbookRepository, IMessageSender messageSender)
        {
            _guestbookRepository = guestbookRepository;
            _messageSender = messageSender;
        }

        public void RecordEntry(Guestbook guestbook, GuestbookEntry newEntry)
        {
            foreach (var entry in guestbook.Entries)
            {
                if (entry.DateTimeCreated.Date == DateTime.UtcNow.Date)
                {
                    _messageSender.SendNotificationEmail(entry.EmailAddress, newEntry.Message);
                }
            }
        }
    }
}
<file_sep>﻿using System;
using CleanArchitecture.Core.Entities;
using CleanArchitecture.Core.Events;
using CleanArchitecture.Core.Interfaces;

namespace CleanArchitecture.Core.Handlers
{
    public class GuestbookNotificationHandler : IHandle<EntryAddedEvent>
    {
        private readonly IRepository<Guestbook> _guestbookRepository;
        private readonly IMessageSender _messageSender;

        public GuestbookNotificationHandler(IRepository<Guestbook> guestbookRepository, IMessageSender messageSender)
        {
            _guestbookRepository = guestbookRepository;
            _messageSender = messageSender;
        }

        public void Handle(EntryAddedEvent entryAddedEvent)
        {
            var guestbook = _guestbookRepository.GetById(entryAddedEvent.GuestbookId);
            foreach (var entry in guestbook.Entries)
            {
                if (entry.DateTimeCreated.Date == DateTime.UtcNow.Date)
                {
                    _messageSender.SendNotificationEmail(entry.EmailAddress, entryAddedEvent.Entry.Message);
                }
            }
        }
    }
}
<file_sep>﻿using System.Linq;
using CleanArchitecture.Core.Entities;
using Microsoft.EntityFrameworkCore;

namespace CleanArchitecture.Infrastructure.Data
{
    public class GuestbookRepository : EfRepository<Guestbook>
    {
        public GuestbookRepository(AppDbContext dbContext) : base(dbContext)
        {
            
        }

        public override Guestbook GetById(int id)
        {
            return _dbContext.Guestbooks.Include(g => g.Entries).FirstOrDefault(g => g.Id == id);
        }
    }
}
