Skip to content

Commit

Permalink
added lazy loading, added craftsman services
Browse files Browse the repository at this point in the history
  • Loading branch information
michaell027 committed Mar 26, 2024
1 parent 96f87fc commit 65ee888
Show file tree
Hide file tree
Showing 30 changed files with 642 additions and 899 deletions.
37 changes: 37 additions & 0 deletions crafts-api/Controllers/CraftersController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using crafts_api.Entities.Dto;
using crafts_api.Entities.Models;
using crafts_api.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace crafts_api.Controllers
{
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class CraftersController : ControllerBase
{
private readonly ICraftersService _craftsmanService;

public CraftersController(ICraftersService craftsmanService) => _craftsmanService = craftsmanService;


[HttpGet ("get-craftsman")]
public async Task<IActionResult> GetCraftsman(Guid craftsmanPublicId)
{
// var token = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");

var craftsmanProfile = await _craftsmanService.GetCraftsmanProfile(craftsmanPublicId);
return Ok(craftsmanProfile);
}

[HttpPost("add-service")]
public async Task<IActionResult> AddService(AddServiceRequest addServiceRequest)
{
var token = Request.Headers["Authorization"].ToString().Replace("Bearer ", "");

await _craftsmanService.AddService(addServiceRequest, token);
return Ok();
}
}
}
67 changes: 67 additions & 0 deletions crafts-api/Entities/DbInitializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using crafts_api.context;
using crafts_api.Entities.Domain;
using crafts_api.Entities.Enum;
using crafts_api.Entities.Models;
using crafts_api.models.domain;
using crafts_api.models.models;
using Microsoft.AspNetCore.Identity;
Expand Down Expand Up @@ -79,7 +80,73 @@ public static async Task Seed(DatabaseContext context, UserManager<IdentityUser>
transaction.Rollback();
}
}
}

if (!context.Crafters.Any())
{
var registerCraftsmanRequest = new RegisterCraftsmanRequest
{
FirstName = "Jozef",
LastName = "Jozefovic",
Username = "jozef123",
Email = "jozef123@post.sk",
Password = "jozef123",
PasswordConfirmation = "jozef123",
Bio = "I am a craftsmen, I love to create things",
PhoneNumber = "0901234567",
City = "Bratislava",
Country = "Slovakia",
Number = "63",
PostalCode = "84104",
Street = "Karloveska"
};

var identityUser = new IdentityUser
{
UserName = registerCraftsmanRequest.Username,
Email = registerCraftsmanRequest.Email
};

var craftsmanProfile = new CraftsmanProfile
{
Bio = registerCraftsmanRequest.Bio,
PhoneNumber = registerCraftsmanRequest.PhoneNumber,
City = registerCraftsmanRequest.City,
Country = registerCraftsmanRequest.Country,
Street = registerCraftsmanRequest.Street,
Number = registerCraftsmanRequest.Number,
PostalCode = registerCraftsmanRequest.PostalCode
};

var craftsman = new Craftsman
{
PublicId = Guid.NewGuid(),
IdentityId = identityUser.Id,
Username = registerCraftsmanRequest.Username,
FirstName = registerCraftsmanRequest.FirstName,
LastName = registerCraftsmanRequest.LastName,
Email = registerCraftsmanRequest.Email,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
Role = Role.Crafter,
CraftsmanProfile = craftsmanProfile
};

using (var transaction = context.Database.BeginTransaction())
{
var result = await userManager.CreateAsync(identityUser, registerCraftsmanRequest.Password);

if (result.Succeeded)
{
await context.Crafters.AddAsync(craftsman);
await context.SaveChangesAsync();
transaction.Commit();
}
else
{
transaction.Rollback();
}
}
}

if (!context.Categories.Any())
Expand Down
4 changes: 2 additions & 2 deletions crafts-api/Entities/Domain/Category.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ public class Category
public Guid PublicId { get; set; }

// name
public string Name { get; set; }
public required string Name { get; set; }

// sk_name
public string SkName { get; set; }
public required string SkName { get; set; }

// to dto
public CategoryDto ToDto() => new()
Expand Down
15 changes: 8 additions & 7 deletions crafts-api/Entities/Domain/Craftsman.cs
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
using crafts_api.Entities.Enum;
using crafts_api.models.domain;
using System.ComponentModel.DataAnnotations;

namespace crafts_api.Entities.Domain
{
public class Craftsman
{
// id
[Key]
public int Id { get; set; }
// public id
public Guid PublicId { get; set; }
// identity_id
public string IdentityId { get; set; }
public required string IdentityId { get; set; }
// first_name
public string FirstName { get; set; }
public required string FirstName { get; set; }
// last_name
public string LastName { get; set; }
public required string LastName { get; set; }
// email
public string Email { get; set; }
public required string Email { get; set; }
// created_at
public DateTime CreatedAt { get; set; }
// updated_at
public DateTime UpdatedAt { get; set; }
// role
public Role Role { get; set; }
// username
public string Username { get; set; }
public required string Username { get; set; }
// craftsman_profile
public CraftsmanProfile CraftsmanProfile { get; set; }
public virtual required CraftsmanProfile CraftsmanProfile { get; set; }
// craftsman_reviews
// public List<CraftsmanReview> CraftsmanReviews { get; set; }
// craftsman_ratings
Expand Down
22 changes: 11 additions & 11 deletions crafts-api/Entities/Domain/CraftsmanProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,26 @@ public class CraftsmanProfile
public int Id { get; set; }
// public id
public Guid CraftsmanPublicId { get; set; }
public Craftsman Craftsman { get; set; }
public virtual Craftsman Craftsman { get; set; } = null!;
// description
public string Bio { get; set; }
public string Bio { get; set; } = string.Empty;
// phone_number
public string PhoneNumber { get; set; }
public required string PhoneNumber { get; set; }
// address
public string Address { get; set; }
public string Address { get; set; } = string.Empty;
// city
public string City { get; set; }
public required string City { get; set; }
// country
public string Country { get; set; }
public required string Country { get; set; }
// street
public string Street { get; set; }
public required string Street { get; set; }
// number
public string Number { get; set; }
public required string Number { get; set; }
// postal_code
public string PostalCode { get; set; }
public required string PostalCode { get; set; }
// profile_picture
public string ProfilePicture { get; set; }
public string ProfilePicture { get; set; } = string.Empty;
// craftsman_services
public List<CraftsmanService> CraftsmanServices { get; set; }
public virtual List<CraftsmanService> CraftsmanServices { get; set; } = new List<CraftsmanService>();
}
}
4 changes: 2 additions & 2 deletions crafts-api/Entities/Domain/CraftsmanService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ public class CraftsmanService
public int Id { get; set; }
// public id
public Guid CraftsmanProfileCraftsmanPublicId { get; set; }
public CraftsmanProfile CraftsmanProfile { get; set; }
public virtual CraftsmanProfile CraftsmanProfile { get; set; }
// service_public_id
public Guid ServicePublicId { get; set; }
// service
public Service Service { get; set; }
public virtual Service Service { get; set; }
// price
public decimal Price { get; set; }
// duration
Expand Down
2 changes: 1 addition & 1 deletion crafts-api/Entities/Domain/RefreshToken.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class RefreshToken
public DateTime Expires { get; set; }
public string UserIdentityId { get; set; }

public User User { get; set; }
public virtual User User { get; set; }
}
}

2 changes: 1 addition & 1 deletion crafts-api/Entities/Domain/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ public class Service
public string Description { get; set; }
// category
public Guid CategoryPublicId { get; set; }
public Category Category { get; set; }
public virtual Category Category { get; set; }
}
}
2 changes: 1 addition & 1 deletion crafts-api/Entities/Domain/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,6 @@ public class User
//role
public Role Role { get; set; }
//user_profile
public UserProfile UserProfile { get; set; }
public virtual UserProfile UserProfile { get; set; }
}
}
2 changes: 1 addition & 1 deletion crafts-api/Entities/Domain/UserProfile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class UserProfile
public int Id { get; set; }
// public id
public Guid UserPublicId { get; set; }
public User User { get; set; }
public virtual User User { get; set; }
// profile_picture
public string ProfilePicture { get; set; }
// country
Expand Down
17 changes: 17 additions & 0 deletions crafts-api/Entities/Dto/CraftsmanProfileViewDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using crafts_api.Entities.Domain;

namespace crafts_api.Entities.Dto
{
public class CraftsmanProfileViewDto
{
public Guid PublicId { get; set; }
public string Bio { get; set; }
public string PhoneNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string UserName { get; set; }
public DateTime CreatedAt { get; set; }
public string ProfilePicture { get; set; }
public List<CraftsmanServiceDto> CraftsmanServices { get; set; } = new List<CraftsmanServiceDto>();
}
}
20 changes: 20 additions & 0 deletions crafts-api/Entities/Dto/CraftsmanServiceDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
namespace crafts_api.Entities.Dto
{
public class CraftsmanServiceDto
{
// public id
public Guid ServicePublicId { get; set; }
// name
public string Name { get; set; }
// description
public string Description { get; set; }
// category
public Guid CategoryPublicId { get; set; }
public string CategoryName { get; set; }
// price
public decimal Price { get; set; }
// duration
public int Duration { get; set; }

}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
using crafts_api.Entities.Enum;

namespace crafts_api.models.dto;

public class UserDto
public class LoggedUserDto
{
public Guid PublicId { get; set; }
public string UserName { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Email { get; set; }
public DateTime CreatedAt { get; set; }
public Role Role { get; set; }
}
11 changes: 11 additions & 0 deletions crafts-api/Entities/Models/AddServiceRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace crafts_api.Entities.Models
{
public class AddServiceRequest
{
public string Name { get; set; }
public string Description { get; set; }
public Guid CategoryPublicId { get; set; }
public decimal Price { get; set; }
public int Duration { get; set; }
}
}
2 changes: 1 addition & 1 deletion crafts-api/Entities/Models/LoggedUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ namespace crafts_api.Entities.Models
{
public class LoggedUser
{
public UserDto User { get; set; }
public LoggedUserDto User { get; set; }
public string Token { get; set; }
}
}
3 changes: 3 additions & 0 deletions crafts-api/Entities/Models/LoginRequest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using crafts_api.Entities.Enum;

namespace crafts_api.models.models;

public class LoginRequest
{
public required string Email { get; set; }
public required string Password { get; set; }
public Role Role { get; set; }
}
18 changes: 18 additions & 0 deletions crafts-api/Entities/Models/RegisterCraftsmanRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,23 @@
{
public class RegisterCraftsmanRequest
{
// craftsman
public required string FirstName { get; set; }
public required string LastName { get; set; }
public required string Username { get; set; }
public required string Email { get; set; }
public required string Password { get; set; }
public required string PasswordConfirmation { get; set; }

// craftsman_profile
public string Bio { get; set; } = string.Empty;
public required string PhoneNumber { get; set; }
public string Address { get; set; } = string.Empty;
public required string City { get; set; }
public required string Country { get; set; }
public required string Street { get; set; }
public required string Number { get; set; }
public required string PostalCode { get; set; }
public string ProfilePicture { get; set; } = string.Empty;
}
}
11 changes: 11 additions & 0 deletions crafts-api/Interfaces/ICraftersService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using crafts_api.Entities.Dto;
using crafts_api.Entities.Models;

namespace crafts_api.Interfaces
{
public interface ICraftersService
{
Task AddService(AddServiceRequest addServiceRequest, string token);
Task<CraftsmanProfileViewDto> GetCraftsmanProfile(Guid craftsmanPublicId);
}
}
Loading

0 comments on commit 65ee888

Please sign in to comment.