Skip to content

Commit

Permalink
Baditsa/some improvements (#750)
Browse files Browse the repository at this point in the history
* Validation attribute for enums was added

* Fix mapping

* Set default value for workshop.Status
  • Loading branch information
valerabad authored Jul 14, 2022
1 parent 46570f8 commit d07ed38
Show file tree
Hide file tree
Showing 17 changed files with 29 additions and 37 deletions.
2 changes: 2 additions & 0 deletions OutOfSchool/OutOfSchool.Common/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,6 @@ public static class Constants
public const string PasswordRegexViewModel = @"^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{6,}$";

public const string AddressSeparator = ", ";

public const string EnumErrorMessage = "{0} should be in enum range";
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ public async Task<IActionResult> Create(WorkshopDTO dto)

dto.Id = default;
dto.Address.Id = default;
dto.Status = WorkshopStatus.Open;

if (dto.Teachers != null)
{
foreach (var teacher in dto.Teachers)
Expand Down
2 changes: 1 addition & 1 deletion OutOfSchool/OutOfSchool.WebApi/Models/ApplicationDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ApplicationDto
{
public Guid Id { get; set; }

[JsonConverter(typeof(StringEnumConverter))]
[EnumDataType(typeof(ApplicationStatus), ErrorMessage = Constants.EnumErrorMessage)]
public ApplicationStatus Status { get; set; } = ApplicationStatus.Pending;

[MaxLength(500)]
Expand Down
2 changes: 1 addition & 1 deletion OutOfSchool/OutOfSchool.WebApi/Models/ChildDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class ChildDto
public DateTime DateOfBirth { get; set; } = default;

[Required(ErrorMessage = "Gender is required")]
[Range(0, 1)]
[EnumDataType(typeof(Gender), ErrorMessage = Constants.EnumErrorMessage)]
public Gender Gender { get; set; } = default;

[MaxLength(500)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ public class NotificationDto
public Dictionary<string, string> Data { get; set; } = new Dictionary<string, string>();

[Required]
[EnumDataType(typeof(NotificationType), ErrorMessage = Constants.EnumErrorMessage)]
public NotificationType Type { get; set; }

[Required]
[EnumDataType(typeof(NotificationAction), ErrorMessage = Constants.EnumErrorMessage)]
public NotificationAction Action { get; set; }

[Required]
Expand Down
4 changes: 3 additions & 1 deletion OutOfSchool/OutOfSchool.WebApi/Models/ProviderAdminDto.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using OutOfSchool.WebApi.Enums;
using System.ComponentModel.DataAnnotations;
using OutOfSchool.WebApi.Enums;

namespace OutOfSchool.WebApi.Models;

Expand All @@ -18,5 +19,6 @@ public class ProviderAdminDto

public bool IsDeputy { get; set; }

[EnumDataType(typeof(AccountStatus), ErrorMessage = Constants.EnumErrorMessage)]
public AccountStatus AccountStatus { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,20 +71,22 @@ public class ProviderDto
[MaxLength(30)]
public string Founder { get; set; } = string.Empty;

// TODO: validation of the enum fields ?
[Required]
[EnumDataType(typeof(OwnershipType), ErrorMessage = Constants.EnumErrorMessage)]
public OwnershipType Ownership { get; set; }

// TODO: validation of the enum fields ?
[Required]
[EnumDataType(typeof(ProviderType), ErrorMessage = Constants.EnumErrorMessage)]
public ProviderType Type { get; set; }

[Required]
[EnumDataType(typeof(ProviderStatus), ErrorMessage = Constants.EnumErrorMessage)]
public ProviderStatus Status { get; set; }

[MaxLength(30)]
public string License { get; set; }

[EnumDataType(typeof(ProviderLicenseStatus), ErrorMessage = Constants.EnumErrorMessage)]
public ProviderLicenseStatus LicenseStatus { get; set; }

[MaxLength(256)]
Expand Down Expand Up @@ -121,6 +123,7 @@ public class ProviderDto
public InstitutionDto Institution { get; set; }

[Required]
[EnumDataType(typeof(InstitutionType), ErrorMessage = Constants.EnumErrorMessage)]
public InstitutionType InstitutionType { get; set; }

[ModelBinder(BinderType = typeof(JsonModelBinder))]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class ProviderLicenseStatusDto
public Guid ProviderId { get; set; }

[Required]
[EnumDataType(typeof(ProviderLicenseStatus), ErrorMessage = Constants.EnumErrorMessage)]
public ProviderLicenseStatus LicenseStatus { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@ public class ProviderStatusDto
public Guid ProviderId { get; set; }

[Required]
[EnumDataType(typeof(ProviderStatus), ErrorMessage = Constants.EnumErrorMessage)]
public ProviderStatus Status { get; set; }
}
2 changes: 1 addition & 1 deletion OutOfSchool/OutOfSchool.WebApi/Models/RatingDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class RatingDto
public int Rate { get; set; }

[Required]
[Range(1, 2, ErrorMessage = "The type field should be 1 or 2")]
[EnumDataType(typeof(RatingType), ErrorMessage = Constants.EnumErrorMessage)]
public RatingType Type { get; set; }

[Required]
Expand Down
5 changes: 2 additions & 3 deletions OutOfSchool/OutOfSchool.WebApi/Models/ShortApplicationDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,9 @@ public class ShortApplicationDto
public Guid Id { get; set; }

[Required]
// TODO: remove range and use IsDefined instead
[Range(1, 7, ErrorMessage = "Status should be from 1 to 7")]
[EnumDataType(typeof(ApplicationStatus), ErrorMessage = Constants.EnumErrorMessage)]
public ApplicationStatus Status { get; set; } = ApplicationStatus.Pending;

[MaxLength(500)]
public string RejectionMessage { get; set; }
}
}
2 changes: 1 addition & 1 deletion OutOfSchool/OutOfSchool.WebApi/Models/ShortUserDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ShortUserDto
public string FirstName { get; set; }

[Required(ErrorMessage = "Gender is required")]
[Range(0, 1)]
[EnumDataType(typeof(Gender), ErrorMessage = Constants.EnumErrorMessage)]
public Gender Gender { get; set; } = default;

public string Role { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class TeacherDTO
public string MiddleName { get; set; } = string.Empty;

[Required(ErrorMessage = "Gender is required")]
[Range(0, 1)]
[EnumDataType(typeof(Gender), ErrorMessage = Constants.EnumErrorMessage)]
public Gender Gender { get; set; } = default;

[Required(ErrorMessage = "Date of birth is required")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,21 @@ public class WorkshopDTO : IValidatableObject
[MaxLength(60)]
public string ProviderTitle { get; set; } = string.Empty;

[EnumDataType(typeof(OwnershipType), ErrorMessage = Constants.EnumErrorMessage)]
public OwnershipType ProviderOwnership { get; set; } = OwnershipType.State;

[ModelBinder(BinderType = typeof(JsonModelBinder))]
public IEnumerable<string> Keywords { get; set; } = default;

[Required]
[EnumDataType(typeof(PayRateType), ErrorMessage = Constants.EnumErrorMessage)]
public PayRateType PayRate { get; set; }

public float Rating { get; set; }

public int NumberOfRatings { get; set; }

[JsonConverter(typeof(StringEnumConverter))]
[EnumDataType(typeof(WorkshopStatus), ErrorMessage = Constants.EnumErrorMessage)]
public WorkshopStatus Status { get; set; } = WorkshopStatus.Open;

[Required]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ public class WorkshopStatusDto
public Guid WorkshopId { get; set; }

[Required]
[EnumDataType(typeof(WorkshopStatus))]
[EnumDataType(typeof(WorkshopStatus), ErrorMessage = Constants.EnumErrorMessage)]
public WorkshopStatus Status { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public async Task<WorkshopDTO> Create(WorkshopDTO dto)

var workshop = mapper.Map<Workshop>(dto);
workshop.Teachers = dto.Teachers.Select(dtoTeacher => mapper.Map<Teacher>(dtoTeacher)).ToList();
workshop.Status = WorkshopStatus.Open;

Func<Task<Workshop>> operation = async () =>
await workshopRepository.Create(workshop).ConfigureAwait(false);
Expand Down
25 changes: 2 additions & 23 deletions OutOfSchool/OutOfSchool.WebApi/Util/MappingProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,30 +147,9 @@ public MappingProfile()
.ForMember(dtr => dtr.Workdays, cfg => cfg.MapFrom(dtr => dtr.Workdays.ToDaysBitMask()))
.ForMember(dest => dest.WorkshopId, opt => opt.Ignore());

CreateMap<Application, ApplicationDto>().ReverseMap();
CreateMap<Application, ApplicationDto>();

CreateMap<WorkshopCard, Workshop>()
.ForMember(dest => dest.Id, opt => opt.Ignore())
.ForMember(dest => dest.Phone, opt => opt.Ignore())
.ForMember(dest => dest.Email, opt => opt.Ignore())
.ForMember(dest => dest.Website, opt => opt.Ignore())
.ForMember(dest => dest.Facebook, opt => opt.Ignore())
.ForMember(dest => dest.Instagram, opt => opt.Ignore())
.ForMember(dest => dest.Direction, opt => opt.Ignore())
.ForMember(dest => dest.WorkshopDescriptionItems, opt => opt.Ignore())
.ForMember(dest => dest.DisabilityOptionsDesc, opt => opt.Ignore())
.ForMember(dest => dest.Keywords, opt => opt.Ignore())
.ForMember(dest => dest.DepartmentId, opt => opt.Ignore())
.ForMember(dest => dest.ClassId, opt => opt.Ignore())
.ForMember(dest => dest.InstitutionHierarchy, opt => opt.Ignore())
.ForMember(dest => dest.Class, opt => opt.Ignore())
.ForMember(dest => dest.Teachers, opt => opt.Ignore())
.ForMember(dest => dest.Applications, opt => opt.Ignore())
.ForMember(dest => dest.DateTimeRanges, opt => opt.Ignore())
.ForMember(dest => dest.ChatRooms, opt => opt.Ignore())
.ForMember(dest => dest.Images, opt => opt.Ignore())
.ForMember(dest => dest.Provider, opt => opt.Ignore())
.ForMember(dest => dest.ProviderAdmins, opt => opt.Ignore());
CreateMap<ApplicationDto, Application>().ForMember(dest => dest.Workshop, opt => opt.Ignore());

CreateMap<Workshop, WorkshopCard>()
.ForMember(dest => dest.WorkshopId, opt => opt.MapFrom(s => s.Id))
Expand Down

0 comments on commit d07ed38

Please sign in to comment.