Skip to content

Commit

Permalink
create method for finding a user... add new items to the seed
Browse files Browse the repository at this point in the history
  • Loading branch information
michaell027 committed Apr 9, 2024
1 parent 65ee888 commit 41e6de7
Show file tree
Hide file tree
Showing 26 changed files with 1,315 additions and 391 deletions.
18 changes: 0 additions & 18 deletions crafts-api/Controllers/TestController.cs

This file was deleted.

123 changes: 81 additions & 42 deletions crafts-api/Entities/DbInitializer.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@

using crafts_api.context;
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;
using Microsoft.EntityFrameworkCore;

namespace crafts_api.models
{
Expand Down Expand Up @@ -59,26 +59,24 @@ public static async Task Seed(DatabaseContext context, UserManager<IdentityUser>
FirstName = registerRequest.FirstName,
LastName = registerRequest.LastName,
Email = registerRequest.Email,
CreatedAt = System.DateTime.Now,
UpdatedAt = System.DateTime.Now,
CreatedAt = DateTime.Now,
UpdatedAt = DateTime.Now,
Role = Role.User,
UserProfile = userProfile
};

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

if (result.Succeeded)
{
await context.Users.AddAsync(user);
await context.SaveChangesAsync();
transaction.Commit();
}
else
{
transaction.Rollback();
}
var result = await userManager.CreateAsync(identityUser, registerRequest.Password);
if (result.Succeeded)
{
await context.Users.AddAsync(user);
await context.SaveChangesAsync();
await transaction.CommitAsync();
}
else
{
await transaction.RollbackAsync();
}
}

Expand Down Expand Up @@ -131,91 +129,132 @@ public static async Task Seed(DatabaseContext context, UserManager<IdentityUser>
Role = Role.Crafter,
CraftsmanProfile = craftsmanProfile
};

using (var transaction = context.Database.BeginTransaction())

var category = await context.Categories.FirstOrDefaultAsync( c => c.Name == "Woodworking");

if (category == null)
{
var result = await userManager.CreateAsync(identityUser, registerCraftsmanRequest.Password);

if (result.Succeeded)
{
await context.Crafters.AddAsync(craftsman);
await context.SaveChangesAsync();
transaction.Commit();
}
else
category = new Category
{
transaction.Rollback();
}
PublicId = Guid.NewGuid(),
Name = "Woodworking",
SkName = "Drevo"
};
await context.Categories.AddAsync(category);
await context.SaveChangesAsync();
}

var addServiceRequest = new AddServiceRequest
{
Name = "Woodworking",
Description = "I am a craftsmen, I love to create things",
Price = 20,
Duration = 2,
CategoryPublicId = category.PublicId
};

var service = new Service
{
PublicId = Guid.NewGuid(),
Name = addServiceRequest.Name,
Description = addServiceRequest.Description,
CategoryPublicId = addServiceRequest.CategoryPublicId
};

var craftsmanService = new CraftsmanService
{
CraftsmanProfileCraftsmanPublicId = craftsman.PublicId,
ServicePublicId = service.PublicId,
Price = addServiceRequest.Price,
Duration = addServiceRequest.Duration
};

await using var transaction = await context.Database.BeginTransactionAsync();

var result = await userManager.CreateAsync(identityUser, registerCraftsmanRequest.Password);

if (result.Succeeded)
{
await context.Crafters.AddAsync(craftsman);
await context.Services.AddAsync(service);
await context.CraftsmanServices.AddAsync(craftsmanService);
await context.SaveChangesAsync();
await transaction.CommitAsync();
}
else
{
await transaction.RollbackAsync();
}

}

if (!context.Categories.Any())
{
var categories = new List<Category>
{
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Woodworking",
SkName = "Drevo"
},
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Knitting",
SkName = "Pletenie"
},
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Sewing",
SkName = "Šitie"
},
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Pottery",
SkName = "Keramika"
},
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Painting",
SkName = "Maľovanie"
},
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Jewelry",
SkName = "Šperky"
},
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Glass",
SkName = "Sklo"
},
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Metalworking",
SkName = "Kováčstvo"
},
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Paper",
SkName = "Papier"
},
new Category
new ()
{
PublicId = Guid.NewGuid(),
Name = "Leather",
SkName = "Koža"
}
};
context.Categories.AddRange(categories);
context.SaveChanges();
await context.Categories.AddRangeAsync(categories);
await context.SaveChangesAsync();
}
}
}
Expand Down
1 change: 0 additions & 1 deletion crafts-api/Entities/Domain/RefreshToken.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using crafts_api.models.domain;
using System.ComponentModel.DataAnnotations.Schema;

namespace crafts_api.Entities.Domain
{
Expand Down
24 changes: 23 additions & 1 deletion crafts-api/Entities/Domain/User.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using crafts_api.Entities.Domain;
using crafts_api.Entities.Dto;
using crafts_api.Entities.Enum;
using crafts_api.Interfaces;

namespace crafts_api.models.domain
{
Expand All @@ -26,6 +28,26 @@ public class User
//role
public Role Role { get; set; }
//user_profile
public virtual UserProfile UserProfile { get; set; }
public virtual UserProfile UserProfile { get; set; } = new UserProfile();

public UserDto ToDto() => new()
{
PublicId = PublicId,
Username = Username,
FirstName = FirstName,
LastName = LastName,
Email = Email,
CreatedAt = CreatedAt,
UpdatedAt = UpdatedAt,
Role = Role,
ProfilePicture = UserProfile.ProfilePicture,
Country = UserProfile.Country,
City = UserProfile.City,
Address = UserProfile.Address,
Street = UserProfile.Street,
Number = UserProfile.Number,
PostalCode = UserProfile.PostalCode,
PhoneNumber = UserProfile.PhoneNumber
};
}
}
23 changes: 23 additions & 0 deletions crafts-api/Entities/Dto/UserDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using crafts_api.Entities.Enum;

namespace crafts_api.Entities.Dto;

public class UserDto
{
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 DateTime UpdatedAt { get; set; }
public Role Role { get; set; }
public string ProfilePicture { get; set; }
public string Country { get; set; }
public string City { get; set; }
public string? Address { get; set; }
public string Street { get; set; }
public string Number { get; set; }
public string PostalCode { get; set; }
public string PhoneNumber { get; set; }
}
1 change: 1 addition & 0 deletions crafts-api/Entities/Models/LoggedUser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ public class LoggedUser
{
public LoggedUserDto User { get; set; }
public string Token { get; set; }
public string RefreshToken { get; set; }
}
}
2 changes: 1 addition & 1 deletion crafts-api/Entities/Models/RegisterUserRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class RegisterUserRequest
public string ProfilePicture { get; set; }

Check warning on line 16 in crafts-api/Entities/Models/RegisterUserRequest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'ProfilePicture' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string Country { get; set; }

Check warning on line 17 in crafts-api/Entities/Models/RegisterUserRequest.cs

View workflow job for this annotation

GitHub Actions / build

Non-nullable property 'Country' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public string City { get; set; }
public string Address { get; set; }
public string Address { get; set; } = string.Empty;
public string Street { get; set; }
public string Number { get; set; }
public string PostalCode { get; set; }
Expand Down
31 changes: 31 additions & 0 deletions crafts-api/Entities/Models/UpdateUserProfileRequest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
namespace crafts_api.Entities.Models
{
public class UpdateUserProfileRequest
{
//username
public string Username { get; set; } = string.Empty;
//first_name
public string FirstName { get; set; } = string.Empty;
//last_name
public string LastName { get; set; } = string.Empty;
//email
public string Email { get; set; } = string.Empty;
//profile_picture
public string ProfilePicture { get; set; } = string.Empty;
//country
public string Country { get; set; } = string.Empty;
//city
public string City { get; set; } = string.Empty;
//address
public string Address { get; set; } = string.Empty;
//street
public string Street { get; set; } = string.Empty;
//number
public string Number { get; set; } = string.Empty;
//postal_code
public string PostalCode { get; set; } = string.Empty;
//phone_number
public string PhoneNumber { get; set; } = string.Empty;

}
}
2 changes: 1 addition & 1 deletion crafts-api/Exceptions/DefaultException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace crafts_api.exceptions
{
public class DefaultException : Exception, IEventSchedulerException
public class DefaultException : Exception
{
public HttpStatusCode? StatusCode { get; set; }
public int ErrorCode { get; set; }
Expand Down
7 changes: 5 additions & 2 deletions crafts-api/Interfaces/IAuthService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,11 @@ namespace crafts_api.interfaces
public interface IAuthService
{
Task CraftsmanRegister(RegisterCraftsmanRequest registerCraftsmanRequest);
Task UserRegister(RegisterUserRequest registerRequest);
Task<LoggedUser> Login(LoginRequest loginRequest);
Task<string> RefreshToken(RefreshTokenRequest refreshTokenRequest);
Task UserRegister(RegisterUserRequest registerRequest);
Task<LoggedUser> RefreshToken(RefreshTokenRequest refreshTokenRequest);
Task UpdateUserProfile(UpdateUserProfileRequest updateProfileRequest, string token);
Task Revoke(string Token);

}
}
16 changes: 0 additions & 16 deletions crafts-api/Interfaces/IEventSchedulerException.cs

This file was deleted.

9 changes: 9 additions & 0 deletions crafts-api/Interfaces/IUserService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using crafts_api.Entities.Dto;
using crafts_api.Entities.Models;

namespace crafts_api.Interfaces;

public interface IUserService
{
public Task<UserDto> GetUser(Guid publicId);
}
Loading

0 comments on commit 41e6de7

Please sign in to comment.