Skip to content

Commit

Permalink
Tidy up question types and remove warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterKnealeCMRI committed Jun 29, 2024
1 parent c7b8093 commit c17f04d
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 70 deletions.
4 changes: 2 additions & 2 deletions src/Simple.App/Surveys/Queries/ListQuestions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ public async Task<IEnumerable<Result>> Handle(Query query, CancellationToken can

return survey.Questions.Select(q =>
{
var type = q.Info.Type;
var result = new Result(q.QuestionId, q.Info.Title, q.Info.Mandatory, type);
var type = q.Type.Type;
var result = new Result(q.QuestionId, q.Type.Title, q.Type.Mandatory, type);
return result;
});
}
Expand Down
47 changes: 10 additions & 37 deletions src/Simple.Domain/Surveys/Question.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Simple.Domain.Surveys;
using Simple.Domain.Surveys.Questions;

namespace Simple.Domain.Surveys;

public class Question
{
Expand All @@ -7,55 +9,26 @@ private Question()
// EF
}

private Question(SurveyId surveyId, QuestionInfo info)
private Question(SurveyId surveyId, QuestionType type)
{
SurveyId = surveyId;
QuestionId = new QuestionId(Guid.NewGuid());
Info = info;
Type = type;
CreatedAt = SystemTime.UtcNow();
}

public static Question CreateTextQuestion(SurveyId surveyId, string title, bool mandatory, int maxLength)
{
return new Question(surveyId, new TextQuestionInfo
{
Title = title,
Mandatory = mandatory,
MaxLength = maxLength
});
return new Question(surveyId, new TextQuestionType(title, mandatory, maxLength));
}

public static Question CreateListQuestion(SurveyId surveyId, string title, bool mandatory, IEnumerable<string> options, bool single)
{
return new Question(surveyId, new ListQuestionInfo
{
Title = title,
Mandatory = mandatory,
Options = options,
Single = single
});
return new Question(surveyId, new ListQuestionType(title, mandatory, options, single));
}

public SurveyId SurveyId { get; init; }
public QuestionInfo Info { get; init; }
public QuestionId QuestionId { get; init; }
public SurveyId SurveyId { get; init; } = null!;
public QuestionType Type { get; init; } = null!;
public QuestionId QuestionId { get; init; } = null!;
public DateTimeOffset CreatedAt { get; init; }
}

public abstract class QuestionInfo(string type)
{
public string Type { get; init; } = type;
public string Title { get; init; }
public bool Mandatory { get; init; }
}

public class ListQuestionInfo() : QuestionInfo("list")
{
public IEnumerable<string> Options { get; init; }
public bool Single { get; init; }
}

public class TextQuestionInfo() : QuestionInfo("text")
{
public int MaxLength { get; init; }
}
14 changes: 14 additions & 0 deletions src/Simple.Domain/Surveys/Questions/ListQuestionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Simple.Domain.Surveys.Questions;

public class ListQuestionType(string title, bool mandatory, IEnumerable<string> options, bool single)
: QuestionType(QuestionTypeNames.List, title, mandatory)
{
public IEnumerable<string> Options { get; init; } = options;
public bool Single { get; init; } = single;
}

public static class QuestionTypeNames
{
public const string List = "list";
public const string Text = "text";
}
8 changes: 8 additions & 0 deletions src/Simple.Domain/Surveys/Questions/QuestionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace Simple.Domain.Surveys.Questions;

public abstract class QuestionType(string type, string title, bool mandatory)
{
public string Type { get; private init; } = type;
public string Title { get; private init; } = title;
public bool Mandatory { get; private init; } = mandatory;
}
7 changes: 7 additions & 0 deletions src/Simple.Domain/Surveys/Questions/TextQuestionType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
namespace Simple.Domain.Surveys.Questions;

public class TextQuestionType(string title, bool mandatory, int maxLength)
: QuestionType(QuestionTypeNames.Text, title, mandatory)
{
public int MaxLength { get; init; } = maxLength;
}
6 changes: 3 additions & 3 deletions src/Simple.Domain/Surveys/Survey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,11 @@ public void RemoveQuestion(QuestionId questionId)
_questions.Remove(_questions.Single(q => q.QuestionId == questionId));
}

public SurveyId SurveyId { get; init; }
public SurveyId SurveyId { get; init; } = null!;

public SurveyName Name { get; init; }
public SurveyName Name { get; init; } = null!;

public TenantId TenantId { get; init; }
public TenantId TenantId { get; init; } = null!;

public DateTimeOffset CreatedAt { get; init; }

Expand Down
13 changes: 3 additions & 10 deletions src/Simple.Domain/Tenants/Tenant.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
using Simple.Domain.Surveys;
using Simple.Domain.Users;
namespace Simple.Domain.Tenants;

namespace Simple.Domain.Tenants;

public partial class Tenant : IAggregateRoot
public class Tenant : IAggregateRoot
{
private Tenant()
{
Expand All @@ -17,13 +14,9 @@ public Tenant(TenantId id, TenantName name)
CreatedAt = SystemTime.UtcNow();
}

public TenantId TenantId { get; private init; }
public TenantId TenantId { get; private init; } = null!;

public TenantName TenantName { get; private init; } = null!;

public DateTimeOffset CreatedAt { get; private init; }

public virtual ICollection<Survey> Surveys { get; set; } = new List<Survey>();

public virtual ICollection<User> Users { get; set; } = new List<User>();
}
4 changes: 2 additions & 2 deletions src/Simple.Domain/Users/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public void ChangeName(Name name)
Name = name;
}

public UserId UserId { get; init; }
public UserId UserId { get; init; } = null!;

public TenantId TenantId { get; init; }
public TenantId TenantId { get; init; } = null!;

public Name Name { get; private set; } = null!;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public void Configure(EntityTypeBuilder<Question> builder)
.HasColumnName(SurveyIdColumn)
.HasConversion<SurveyIdConverter>();

builder.Property(e => e.Info)
builder.Property(e => e.Type)
.HasColumnName(InfoColumn)
.HasColumnType("jsonb")
.HasConversion<QuestionInfoConverter>();
Expand Down
27 changes: 12 additions & 15 deletions src/Simple.Infra/Database/Converters/QuestionInfoConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,37 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Simple.Domain.Surveys;
using Simple.Domain.Surveys.Questions;

namespace Simple.Infra.Database.Converters;

public class QuestionInfoConverter() : ValueConverter<QuestionInfo, string>(
public class QuestionInfoConverter() : ValueConverter<QuestionType, string>(
x => JsonConvert.SerializeObject(x),
x => JsonConvert.DeserializeObject<QuestionInfo>(x, new QuestionInfoDeserializer())!
x => JsonConvert.DeserializeObject<QuestionType>(x, new QuestionInfoDeserializer())!
);

public class QuestionInfoDeserializer : JsonConverter
{
public override bool CanConvert(Type objectType)
{
return typeof(QuestionInfo).IsAssignableFrom(objectType);
return typeof(QuestionType).IsAssignableFrom(objectType);
}

public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
public override object ReadJson(JsonReader reader, Type objectType, object? existingValue, JsonSerializer serializer)
{
JObject jo = JObject.Load(reader);

var jo = JObject.Load(reader);
var type = (string)jo["Type"]!;
switch (type)
return type switch
{
case "text":
return jo.ToObject<TextQuestionInfo>()!;
case "list":
return jo.ToObject<ListQuestionInfo>()!;
default:
throw new NotSupportedException($"Type is not supported '{type}'");
}
QuestionTypeNames.Text => jo.ToObject<TextQuestionType>()!,
QuestionTypeNames.List => jo.ToObject<ListQuestionType>()!,
_ => throw new NotSupportedException($"Type is not supported '{type}'")
};
}

public override bool CanWrite => false;

public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
public override void WriteJson(JsonWriter writer, object? value, JsonSerializer serializer)
{
throw new NotImplementedException();
}
Expand Down

0 comments on commit c17f04d

Please sign in to comment.