Skip to content

Commit

Permalink
Added highlights
Browse files Browse the repository at this point in the history
  • Loading branch information
DavidLazarescu committed Sep 11, 2023
1 parent 72160f6 commit ef3cc48
Show file tree
Hide file tree
Showing 23 changed files with 1,642 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/Application/Common/DTOs/Books/BookOutDto.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using Application.Common.DTOs.Highlights;
using Application.Common.DTOs.Tags;

namespace Application.Common.DTOs.Books;
Expand Down Expand Up @@ -38,4 +39,6 @@ public class BookOutDto


public ICollection<TagOutDto> Tags { get; set; } = new List<TagOutDto>();

public ICollection<HighlightOutDto> Highlights { get; set; } = new List<HighlightOutDto>();
}
17 changes: 17 additions & 0 deletions src/Application/Common/DTOs/Highlights/HighlightInDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace Application.Common.DTOs.Highlights;

public class HighlightInDto
{
[Required]
public Guid Guid { get; set; }

[Required]
public string Color { get; set; }

[Required]
public int PageNumber { get; set; }

[Required]
public ICollection<RectFInDto> Rects { get; set; } = new List<RectFInDto>();
}
12 changes: 12 additions & 0 deletions src/Application/Common/DTOs/Highlights/HighlightOutDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Application.Common.DTOs.Highlights;

public class HighlightOutDto
{
public string Guid { get; set; }

public string Color { get; set; }

public int PageNumber { get; set; }

public ICollection<RectFOutDto> Rects { get; set; } = new List<RectFOutDto>();
}
17 changes: 17 additions & 0 deletions src/Application/Common/DTOs/Highlights/RectFInDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.ComponentModel.DataAnnotations;
namespace Application.Common.DTOs.Highlights;

public class RectFInDto
{
[Required]
public float X { get; set; }

[Required]
public float Y { get; set; }

[Required]
public float Width { get; set; }

[Required]
public float Height { get; set; }
}
12 changes: 12 additions & 0 deletions src/Application/Common/DTOs/Highlights/RectFOutDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
namespace Application.Common.DTOs.Highlights;

public class RectFOutDto
{
public float X { get; set; }

public float Y { get; set; }

public float Width { get; set; }

public float Height { get; set; }
}
17 changes: 17 additions & 0 deletions src/Application/Common/Mappings/HighlightAutoMapperProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Application.Common.DTOs.Highlights;
using Application.Common.DTOs.Tags;
using AutoMapper;
using Domain.Entities;

namespace Application.Common.Mappings;

public class HighlightAutoMapperProfile : Profile
{
public HighlightAutoMapperProfile()
{
CreateMap<HighlightInDto, Highlight>()
.ForMember(dest => dest.HighlightId, temp => temp.MapFrom(src => src.Guid));
CreateMap<Highlight, HighlightOutDto>()
.ForMember(dest => dest.Guid, temp => temp.MapFrom(src => src.HighlightId.ToString()));
}
}
14 changes: 14 additions & 0 deletions src/Application/Common/Mappings/RectFAutoMapperProfile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Application.Common.DTOs.Highlights;
using AutoMapper;
using Domain.Entities;

namespace Application.Common.Mappings;

public class RectFAutoMapperProfile : Profile
{
public RectFAutoMapperProfile()
{
CreateMap<RectFInDto, RectF>();
CreateMap<RectF, RectFOutDto>();
}
}
11 changes: 11 additions & 0 deletions src/Application/Interfaces/Repositories/IHighlightRepository.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Domain.Entities;

namespace Application.Interfaces.Repositories;

public interface IHighlightRepository
{
public Task<int> SaveChangesAsync();
void Add(Highlight highlight);
void Delete(Highlight highlight);
Task<Highlight> GetAsync(Guid bookId, Guid highlightGuid);
}
9 changes: 9 additions & 0 deletions src/Application/Interfaces/Services/IHighlightService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Application.Common.DTOs.Highlights;

namespace Application.Interfaces.Services;

public interface IHighlightService
{
Task CreateHighlightAsync(string email, Guid bookGuid, HighlightInDto highlightIn);
Task DeleteHighlightAsync(string email, Guid bookGuid, Guid highlightGuid);
}
72 changes: 72 additions & 0 deletions src/Application/Services/HighlightService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Application.Common.DTOs.Highlights;
using Application.Common.Exceptions;
using Application.Interfaces.Repositories;
using Application.Interfaces.Services;
using AutoMapper;
using Domain.Entities;

namespace Application.Services;

public class HighlightService : IHighlightService
{
private readonly IUserRepository _userRepository;
private readonly IMapper _mapper;
private readonly IHighlightRepository _highlightRepository;
private readonly IBookRepository _bookRepository;


public HighlightService(IMapper mapper,
IHighlightRepository highlightRepository,
IBookRepository bookRepository,
IUserRepository userRepository)
{
_mapper = mapper;
_highlightRepository = highlightRepository;
_bookRepository = bookRepository;
_userRepository = userRepository;
}


public async Task CreateHighlightAsync(string email, Guid bookGuid, HighlightInDto highlightIn)
{
var user = await _userRepository.GetAsync(email, trackChanges: true);
var book = user.Books.SingleOrDefault(book => book.BookId == bookGuid);
if (book == default)
{
const string message = "No book with this id exists";
throw new CommonErrorException(404, message, 4);
}

var highlightExists = book.Highlights.Any(h => h.HighlightId == highlightIn.Guid);
if(highlightExists)
{
const string message = "A highlight with this id already exists";
throw new CommonErrorException(409, message, 0);
}

var newHighlight = _mapper.Map<Highlight>(highlightIn);
newHighlight.Book = book;
book.Highlights.Add(newHighlight);

await _highlightRepository.SaveChangesAsync();
}

public async Task DeleteHighlightAsync(string email, Guid bookGuid, Guid highlightGuid)
{
var user = await _userRepository.GetAsync(email, trackChanges: true);
var bookExists = await _bookRepository.ExistsAsync(user.Id, bookGuid);
if (!bookExists)
return;

var highlight = await _highlightRepository.GetAsync(bookGuid,
highlightGuid);
if (highlight == default)
{
const string message = "No highlight with this id exists";
throw new CommonErrorException(404, message, 0);
}

_highlightRepository.Delete(highlight);
await _highlightRepository.SaveChangesAsync();
}
}
2 changes: 2 additions & 0 deletions src/Domain/Entities/Book.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ public class Book

public ICollection<Tag> Tags { get; set; } = new List<Tag>();

public ICollection<Highlight> Highlights { get; set; } = new List<Highlight>();

public string UserId { get; set; }
public User User { get; set; }
}
23 changes: 23 additions & 0 deletions src/Domain/Entities/Highlight.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace Domain.Entities;

public class Highlight
{
[DatabaseGenerated(DatabaseGeneratedOption.None)]
[Key]
public Guid HighlightId { get; set; }

[Required]
public string Color { get; set; }

[Required]
public int PageNumber { get; set; }

[Required]
public ICollection<RectF> Rects { get; set; } = new List<RectF>();

public Guid BookId { get; set; }
public Book Book { get; set; }
}
24 changes: 24 additions & 0 deletions src/Domain/Entities/RectF.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;

namespace Domain.Entities;

public class RectF
{
[Key]
public Guid RectFId { get; set; }

[Required]
public float X { get; set; }

[Required]
public float Y { get; set; }

[Required]
public float Width { get; set; }

[Required]
public float Height { get; set; }

public Guid HighlightId { get; set; }
public Highlight Highlight { get; set; }
}
1 change: 1 addition & 0 deletions src/Infrastructure/Persistence/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ namespace Infrastructure.Persistence;
public class DataContext : IdentityDbContext<User>
{
public DbSet<Book> Books { get; set; }
public DbSet<Highlight> Highlights { get; set; }
public DbSet<Tag> Tags { get; set; }


Expand Down
Loading

0 comments on commit ef3cc48

Please sign in to comment.