Skip to content

Commit

Permalink
Refactor Hero class and repository
Browse files Browse the repository at this point in the history
  • Loading branch information
0GiS0 committed Feb 9, 2024
1 parent c1d112d commit 7c6efff
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
36 changes: 35 additions & 1 deletion Models/Hero.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
using System.ComponentModel.DataAnnotations;

namespace tour_of_heroes_api.Models
{
/// <summary>
/// Represents a hero in the Tour of Heroes application.
/// </summary>
public class Hero
{
/// <summary>
/// Initializes a new instance of the <see cref="Hero"/> class with the specified name and alter ego.
/// </summary>
/// <param name="Name">The name of the hero.</param>
/// <param name="AlterEgo">The alter ego of the hero.</param>
public Hero(string Name, string AlterEgo)
{
this.Name = Name;
this.AlterEgo = AlterEgo;
}

/// <summary>
/// Gets or sets the ID of the hero.
/// </summary>
[Key]
public int Id { get; set; }

/// <summary>
/// Gets or sets the name of the hero.
/// </summary>
[Required]
public string Name { get; set; }

/// <summary>
/// Gets or sets the alter ego of the hero.
/// </summary>
[Required]
public string AlterEgo { get; set; }
public string Description { get; set; }

/// <summary>
/// Gets or sets the description of the hero.
/// </summary>
public string? Description { get; set; }
}
}
2 changes: 1 addition & 1 deletion interfaces/IHeroRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public interface IHeroRepository
/// </summary>
/// <param name="id">The ID of the hero.</param>
/// <returns>The hero with the specified ID.</returns>
Hero GetById(int id);
Hero? GetById(int id);

/// <summary>
/// Adds a new hero.
Expand Down
8 changes: 6 additions & 2 deletions repositories/HeroRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class HeroRepository : IHeroRepository

public IEnumerable<Hero> GetAll() => _context.Heroes.ToList();

public Hero GetById(int id) => _context.Heroes.FirstOrDefault(h => h.Id == id);
public Hero? GetById(int id) => _context.Heroes.FirstOrDefault(h => h.Id == id);

public void Add(Hero hero)
{
Expand All @@ -19,7 +19,11 @@ public void Add(Hero hero)

public void Delete(int id)
{
_context.Heroes.Remove(GetById(id));
var hero = GetById(id);

if (hero == null) return;

_context.Heroes.Remove(hero);
_context.SaveChanges();
}

Expand Down

0 comments on commit 7c6efff

Please sign in to comment.