Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating a controller and service #1618

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.ComponentModel.DataAnnotations;

namespace OutOfSchool.BusinessLogic.Models;
public class StudySubjectDto
{
public int Id { get; set; }
/// <summary>
/// Name in Ukrainian
/// </summary>
[Required(ErrorMessage = "The name in Ukrainian is required.")]
public string NameInUkrainian { get; set; }

/// <summary>
/// Name in the language of instruction
/// </summary>
[Required(ErrorMessage = "The name in the language of instruction is required.")]
public string NameInInstructionLanguage { get; set; }

/// <summary>
/// Language of instruction (allows multiple selection)
/// </summary>
[Required(ErrorMessage = "The language of instruction is required.")]
public List<Language> Languages { get; set; }
}
43 changes: 43 additions & 0 deletions OutOfSchool/OutOfSchool.BusinessLogic/Services/ISubjectService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using OutOfSchool.BusinessLogic.Enums;
using OutOfSchool.BusinessLogic.Models;

namespace OutOfSchool.BusinessLogic.Services;
public interface ISubjectService
{
/// <summary>
/// Get all entities.
/// </summary>
/// <param name="localization">Localization: Ua - 0, En - 1.</param>
/// <returns>List of all Subjects.</returns>
Task<IEnumerable<StudySubjectDto>> GetAll(LocalizationType localization = LocalizationType.Ua);

/// <summary>
/// Get entity by it's key.
/// </summary>
/// /// <param name="id">Key in the table.</param>
/// <param name="localization">Localization: Ua - 0, En - 1.</param>
/// <returns>Subject.</returns>
Task<StudySubjectDto> GetById(long id, LocalizationType localization = LocalizationType.Ua);

/// <summary>
/// Add entity.
/// </summary>
/// <param name="dto">Tag entity to add.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<StudySubjectDto> Create(StudySubjectDto dto);

/// <summary>
/// Update entity.
/// </summary>
/// /// <param name="dto">Subject entity to add.</param>
/// <param name="localization">Localization: Ua - 0, En - 1.</param>
/// <returns>A <see cref="Task{TResult}"/> representing the result of the asynchronous operation.</returns>
Task<StudySubjectDto> Update(StudySubjectDto dto, LocalizationType localization = LocalizationType.Ua);

/// <summary>
/// Delete entity.
/// </summary>
/// <param name="id">Subject key.</param>
/// <returns>A <see cref="Task"/> representing the result of the asynchronous operation.</returns>
Task Delete(long id);
}
128 changes: 128 additions & 0 deletions OutOfSchool/OutOfSchool.BusinessLogic/Services/SubjectService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using AutoMapper;
using Microsoft.Extensions.Localization;
using OutOfSchool.BusinessLogic.Enums;
using OutOfSchool.BusinessLogic.Models;
using OutOfSchool.BusinessLogic.Models.Tag;
using OutOfSchool.Services.Repository.Base.Api;

namespace OutOfSchool.BusinessLogic.Services;
public class SubjectService : ISubjectService
{
private readonly IEntityRepository<long, StudySubject> repository;
private readonly ILogger<SubjectService> logger;
private readonly IStringLocalizer<SharedResource> localizer;
private readonly IMapper mapper;

/// <summary>
/// Initializes a new instance of the <see cref="SubjectService"/> class.
/// </summary>
/// <param name="repository">Repository.</param>
/// <param name="logger">Logger.</param>
/// <param name="localizer">Localizer.</param>
/// <param name="mapper">Mapper.</param>
public SubjectService(
IEntityRepository<long, StudySubject> repository,
ILogger<SubjectService> logger,
IStringLocalizer<SharedResource> localizer,
IMapper mapper)
{
this.localizer = localizer ?? throw new ArgumentNullException(nameof(localizer));
this.repository = repository ?? throw new ArgumentNullException(nameof(repository));
this.logger = logger ?? throw new ArgumentNullException(nameof(logger));
this.mapper = mapper ?? throw new ArgumentNullException(nameof(mapper));
}

/// <inheritdoc/>
public async Task<StudySubjectDto> Create(StudySubjectDto dto)
{
logger.LogDebug("StudySubject creating was started.");

var tag = mapper.Map<StudySubject>(dto);

var newTag = await repository.Create(tag).ConfigureAwait(false);

logger.LogDebug($"StudySubject with Id = {newTag?.Id} created successfully.");

return mapper.Map<StudySubjectDto>(newTag);
}

/// <inheritdoc/>
public async Task Delete(long id)
{
logger.LogInformation($"Deleting Subject with Id = {id} started.");

var entity = await repository.GetById(id).ConfigureAwait(false);

if (entity is null)
{
logger.LogWarning($"Subject with Id = {id} was not found.");
throw new KeyNotFoundException($"Subject with Id = {id} does not exist.");
}

try
{
await repository.Delete(entity).ConfigureAwait(false);

logger.LogInformation($"Subject with Id = {id} successfully deleted.");
}
catch (DbUpdateConcurrencyException)
{
logger.LogError($"Deleting Subject with Id = {id} failed.");
throw;
}
}

/// <inheritdoc/>
public async Task<IEnumerable<StudySubjectDto>> GetAll(LocalizationType localization = LocalizationType.Ua)
{
logger.LogInformation("Getting all Subjects started.");

var subjects = await repository.GetAll().ConfigureAwait(false);

logger.LogInformation(!subjects.Any()
? "Subject table is empty."
: $"All {subjects.Count()} records were successfully received from the Subject table");

return subjects.Select(subject => mapper.Map<StudySubjectDto>(subject)).ToList();
}

/// <inheritdoc/>
public async Task<StudySubjectDto> GetById(long id, LocalizationType localization = LocalizationType.Ua)
{
logger.LogInformation($"Getting Subject by Id started. Looking Id = {id}.");

var subject = await repository.GetById(id).ConfigureAwait(false);

if (subject == null)
{
throw new ArgumentException(
nameof(id),
paramName: $"There are no recors in subjects table with such id - {id}.");
}

logger.LogInformation($"Got a Subject with Id = {id}.");

return mapper.Map<StudySubjectDto>(subject);
}

/// <inheritdoc/>
public async Task<StudySubjectDto> Update(StudySubjectDto dto, LocalizationType localization = LocalizationType.Ua)
{
logger.LogDebug($"Updating StudySubject with Id = {dto.Id}, {localization} localization, started.");

var Localized = await repository.GetById(dto.Id).ConfigureAwait(false);

if (Localized == null)
{
logger.LogError($"Updating failed. Tag with Id = {dto.Id} doesn't exist in the system.");

return null;
}

var tag = await repository.Update(Localized).ConfigureAwait(false);

logger.LogDebug($"Tag with Id = {tag.Id} updated succesfully.");

return mapper.Map<StudySubjectDto>(tag);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,20 @@ public static void Seed(this ModelBuilder builder)
Title = "Певний місяць або місяці року",
TitleEn = "A certain month or months of the year",
});

builder.Entity<Language>().HasData(
new Language {
Id = 1,
Title = "English",
Code = "en",
},
new Language
{
Id = 2,
Title = "Українська",
Code = "uk",

});
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;

namespace OutOfSchool.Services.Models.Configurations;
public class SubjectConfiguration : IEntityTypeConfiguration<StudySubject>
{
public void Configure(EntityTypeBuilder<StudySubject> builder)
{
builder.HasKey(x => x.Id);

builder.HasIndex(x => x.IsDeleted);

builder.Property(x => x.IsDeleted).HasDefaultValue(false);

//TO DO
}
}
16 changes: 16 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/Language.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Collections.Generic;

namespace OutOfSchool.Services.Models;

/// <summary>
/// Represents a language used in the educational system.
/// </summary>
public class Language : IKeyedEntity<long>
{
public long Id { get; set; }
public string Code { get; set; }
public string Title { get; set; }

public virtual List<EducationalDiscipline> EducationalDisciplines { get; set; }

Check failure on line 14 in OutOfSchool/OutOfSchool.DataAccess/Models/Language.cs

View workflow job for this annotation

GitHub Actions / SonarCloud

The type or namespace name 'EducationalDiscipline' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 14 in OutOfSchool/OutOfSchool.DataAccess/Models/Language.cs

View workflow job for this annotation

GitHub Actions / SonarCloud

The type or namespace name 'EducationalDiscipline' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 14 in OutOfSchool/OutOfSchool.DataAccess/Models/Language.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

The type or namespace name 'EducationalDiscipline' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 14 in OutOfSchool/OutOfSchool.DataAccess/Models/Language.cs

View workflow job for this annotation

GitHub Actions / test (ubuntu-latest)

The type or namespace name 'EducationalDiscipline' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 14 in OutOfSchool/OutOfSchool.DataAccess/Models/Language.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

The type or namespace name 'EducationalDiscipline' could not be found (are you missing a using directive or an assembly reference?)

Check failure on line 14 in OutOfSchool/OutOfSchool.DataAccess/Models/Language.cs

View workflow job for this annotation

GitHub Actions / test (macOS-latest)

The type or namespace name 'EducationalDiscipline' could not be found (are you missing a using directive or an assembly reference?)
}

36 changes: 36 additions & 0 deletions OutOfSchool/OutOfSchool.DataAccess/Models/StudySubject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;

namespace OutOfSchool.Services.Models;

/// <summary>
/// Represents a subject in the educational system.
/// </summary>
public class StudySubject : IKeyedEntity<long>, ISoftDeleted
{
public long Id { get; set; }
public bool IsDeleted { get; set; }

/// <summary>
/// Name in Ukrainian
/// </summary>
[Required(ErrorMessage = "The name in Ukrainian is required.")]
public string NameInUkrainian { get; set; }

/// <summary>
/// Name in the language of instruction
/// </summary>
[Required(ErrorMessage = "The name in the language of instruction is required.")]
public string NameInInstructionLanguage { get; set; }

/// <summary>
/// Language of instruction (allows multiple selection)
/// </summary>
[Required(ErrorMessage = "The language of instruction is required.")]
public virtual List<Language> Languages { get; set; }

public virtual Workshop WorkshopID{ get; set; }
public virtual Workshop Workshop { get; set; }
}

Loading
Loading