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

Achievements #669

Merged
merged 2 commits into from
Jun 21, 2022
Merged

Achievements #669

merged 2 commits into from
Jun 21, 2022

Conversation

maksgritchin
Copy link
Contributor

No description provided.

@maksgritchin maksgritchin requested a review from a team June 14, 2022 08:53
[Route("api/v{version:apiVersion}/[controller]/[action]")]
public class AchievementController : ControllerBase
{
private readonly IAchievementService service;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use achievementService name instead service, I think

{
public class AchievementTeacher
{
public long Id { get; set; }
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, add AchievementTeacher : IKeyedEntity<long> and the same for AchievementType


public virtual Achievement Achievement { get; set; }

[Required(ErrorMessage = "Title is required")]
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess that ErrorMessage in these types in DataAccessLayer must be irrelevant. The same in Achievement, AchievementType.

[ProducesResponseType(StatusCodes.Status500InternalServerError)]
public async Task<IActionResult> Create(AchievementCreateDTO achievementDto)
{
if (!ModelState.IsValid)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The [ApiController] attribute makes model validation errors automatically trigger an HTTP 400 response. Consequently, the following code is unnecessary in an action method. The same for other methods here.

/// <inheritdoc/>
public async Task<AchievementDto> Create(AchievementCreateDTO dto)
{
logger.LogInformation("Achievement creating was started.");
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please, check dto argument on null here and in Update() method

ILogger<AchievementService> logger,
IStringLocalizer<SharedResource> localizer,
IMapper mapper,
OutOfSchoolDbContext context)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's better to create a related repository rather than pass db context in order to separate repository/service responsibility. Ultimately, with this change you will able to avoid duplicate queries for your methods.


logger.LogInformation($"Achievement with Id = {id} succesfully deleted.");

return Result<AchievementDto>.Success(mapper.Map<AchievementDto>(entity));
Copy link

@ghost ghost Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You wrapped result in custom Result<AchievementDto> but:

  1. It contains only id to return.
  2. You don't define Failed result in this code.
  3. You don't use benefits of such result in your controller action.

I'd better delete this wrapper.
Best way in your case is just make public async Task Delete(Guid id). And maybe I'd extend an exception argument to upstream DbUpdateException. Then if it deletes, you'll be able to send Ok response in controller action (you already has got Id there as parameter if you wanna send it in message) else you'll get an exception in service.

}

/// <inheritdoc/>
public async Task<AchievementDto> Update(AchievementCreateDTO dto)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a comment, not a call to actions. AchievementCreateDTO as parameter in Update method.

achievement.Title = dto.Title;
achievement.AchievementDate = dto.AchievementDate;
achievement.WorkshopId = dto.WorkshopId;
achievement.Children.RemoveAll(x => dto.ChildrenIDs.IndexOf(x.Id) < 0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about achievement.Children.RemoveAll(x => !dto.ChildrenIDs.Contains(x.Id));?
And also check achievement.Teachers.RemoveAll() method below.

achievement.Children.RemoveAll(x => dto.ChildrenIDs.IndexOf(x.Id) < 0);
achievement.Children.AddRange(context.Children.Where(w => dto.ChildrenIDs.Contains(w.Id)).ToList());

achievement.Teachers.RemoveAll(x => dto.Teachers.IndexOf(x.Title) < 0);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

achievement.Teachers.RemoveAll(x => !dto.Teachers.Contains(x.Title));

achievement.Teachers.RemoveAll(x => dto.Teachers.IndexOf(x.Title) < 0);
foreach (var Teacher in dto.Teachers)
{
if (achievement.Teachers.Find(x => x.Title == Teacher) is null)
Copy link

@ghost ghost Jun 14, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. name dto.Teachers which contains strings (that in your case is equal to Title) looks bad
  2. You could rewrite something like this.
var exceptTeachers = dto.Teachers
   .Where(p => achievement.Teachers.All(x => !string.Equals(x.Title, p, StringComparison.InvariantCultureIgnoreCase)));

Or with Except

var exceptTeachers = dto.Teachers
   .Except(achievement.Teachers.Select(x => x.Title));

Then

foreach (var teacherId in exceptTeachers)
{
   achievement.Teachers.Add(new AchievementTeacher { Title = dto.Title, Achievement = achievement });
}

Should compare strings by Equals() method

@ghost
Copy link

ghost commented Jun 14, 2022

And also I haven't seen your new migration considering new Db sets (if you remember, it's Ok :))

@DmyMi DmyMi force-pushed the mhrytc/achievement branch from 9e3b94c to be2c160 Compare June 21, 2022 13:36
@DmyMi DmyMi force-pushed the mhrytc/achievement branch from be2c160 to 9d5cccc Compare June 21, 2022 13:38
@DmyMi DmyMi merged commit 245c3dd into develop Jun 21, 2022
@DmyMi DmyMi deleted the mhrytc/achievement branch June 21, 2022 13:39
@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
5.0% 5.0% Duplication

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants