< Summary

Information
Class: Enjoy.Domain.Jokes.Entities.Joke
Assembly: Enjoy.Domain
File(s): D:\Dotnet\Propuesta-trabajo\Enjoy Project\EnjoyApp\Enjoy\src\Enjoy.Domain\Jokes\Entities\Joke.cs
Line coverage
100%
Covered lines: 64
Uncovered lines: 0
Coverable lines: 64
Total lines: 98
Line coverage: 100%
Branch coverage
100%
Covered branches: 18
Total branches: 18
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
NewId()100%11100%
get_Text()100%11100%
get_AuthorId()100%11100%
get_Origin()100%11100%
get_CreatedOnUtc()100%11100%
get_ModifiedOnUtc()100%11100%
.ctor()100%11100%
get_TopicIds()100%11100%
.ctor(...)100%11100%
Create(...)100%88100%
UpdateText(...)100%44100%
AddTopic(...)100%44100%
RemoveTopic(...)100%22100%

File(s)

D:\Dotnet\Propuesta-trabajo\Enjoy Project\EnjoyApp\Enjoy\src\Enjoy.Domain\Jokes\Entities\Joke.cs

#LineLine coverage
 1using Enjoy.Domain.Shared.Errors;
 2using Enjoy.Domain.Shared.Extensions;
 3using Enjoy.Domain.Shared.Messaging;
 4using Enjoy.Domain.Shared.Primitives;
 5using Enjoy.Domain.Shared.Results;
 6using Enjoy.Domain.Jokes.Events;
 7using Enjoy.Domain.Jokes.ValueObjects;
 8
 9namespace Enjoy.Domain.Jokes.Entities;
 10
 11public sealed class Joke : AggregateRoot, IAuditableEntity
 12{
 2813    public static string NewId() => $"joke_{Guid.CreateVersion7()}";
 14
 3515    public JokeText Text { get; private set; }
 5316    public string AuthorId { get; private set; }
 2717    public Origin Origin { get; private set; }
 18
 319    public DateTime CreatedOnUtc { get; set; }
 320    public DateTime? ModifiedOnUtc { get; set; }
 21
 2622    private readonly List<string> _topicIds = [];
 1223    public IReadOnlyCollection<string> TopicIds => _topicIds.AsReadOnly();
 24
 125    private Joke() : base()
 126    {
 127        AuthorId = null!;
 128        Text = default;
 129        Origin = default;
 130    }
 31
 2532    private Joke(string id, JokeText text, string authorId, Origin origin) : base(id)
 2533    {
 2534        Text = text;
 2535        AuthorId = authorId;
 2536        Origin = origin;
 2537    }
 38
 39    public static Result<Joke> Create(string text, string authorId, string origin)
 3140    {
 3141        List<Error> errors = [];
 42
 3143        if (string.IsNullOrWhiteSpace(authorId))
 444            errors.Add(Error.Joke.AuthorRequired);
 45
 3146        var textResult = JokeText.Create(text);
 3147        if (textResult.IsFailure)
 248            errors.AddRange(textResult.Errors);
 49
 3150        var originResult = Origin.Create(origin);
 3151        if (originResult.IsFailure)
 252            errors.AddRange(originResult.Errors);
 53
 3154        if (!errors.IsEmpty())
 655            return Result<Joke>.Failure(errors);
 56
 2557        Joke joke = new(
 2558            NewId(),
 2559            textResult.Value,
 2560            authorId.Trim(),
 2561            originResult.Value);
 2562        joke.RaiseDomainEvent(new JokeCreatedDomainEvent(DomainEvent.NewId(), joke.Id, joke.AuthorId));
 2563        return Result<Joke>.Success(joke);
 3164    }
 65
 66    public Result UpdateText(string text)
 567    {
 568        var textResult = JokeText.Create(text);
 569        if (textResult.IsFailure)
 270            return Result.Failure(textResult.Errors);
 371        if (Text.Value == textResult.Value.Value)
 172            return Result.Success();
 73
 274        Text = textResult.Value;
 275        RaiseDomainEvent(new JokeTextUpdatedDomainEvent(DomainEvent.NewId(), Id));
 276        return Result.Success();
 577    }
 78
 79    public Result AddTopic(string topicId)
 1880    {
 1881        if (string.IsNullOrWhiteSpace(topicId))
 382            return Result.Failure(Error.Joke.TopicIdRequired);
 1583        if (_topicIds.Contains(topicId))
 184            return Result.Success();
 85
 1486        _topicIds.Add(topicId);
 1487        return Result.Success();
 1888    }
 89
 90    public Result RemoveTopic(string topicId)
 591    {
 592        if (string.IsNullOrWhiteSpace(topicId))
 393            return Result.Success();
 94
 295        _topicIds.Remove(topicId);
 296        return Result.Success();
 597    }
 98}