Skip to content

Commit

Permalink
Merge pull request #26 from LuongXuanNhat/11.Post_Extension
Browse files Browse the repository at this point in the history
#27 | Deloy test sussces
  • Loading branch information
LuongXuanNhat authored Nov 10, 2023
2 parents fa098c1 + 6634db9 commit 921203c
Show file tree
Hide file tree
Showing 28 changed files with 2,329 additions and 123 deletions.
4 changes: 4 additions & 0 deletions VNH.Application/Common/Contants/SystemConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ public static class SystemConstants
{
public const string Token = "Token";
public const string BaseAddress = "BaseAddress";
public const string UrlWeb = "https://vuanhpham25-001-site1.gtempurl.com/";
//// public const string UrlWeb = "https://localhost:7138/";
public const string ConnectString = "Data Source=SQL5110.site4now.net;Initial Catalog=db_aa121e_vuanhpham25;User Id=db_aa121e_vuanhpham25_admin;Password=30102002Mai";
public const string ConnectStringDevelop = "Data Source=.;Initial Catalog=VietNamHistory_2;Integrated Security=True;Encrypt=true;TrustServerCertificate=true;";
}
}
14 changes: 9 additions & 5 deletions VNH.Application/DTOs/Catalog/Posts/PostResponsetDto.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using Microsoft.AspNetCore.Http;
using Microsoft.EntityFrameworkCore.Metadata.Internal;
using System.ComponentModel.DataAnnotations.Schema;
using VNH.Application.DTOs.Catalog.HashTags;
using VNH.Application.DTOs.Catalog.Users;
using VNH.Domain;
Expand All @@ -7,11 +9,13 @@ namespace VNH.Application.DTOs.Catalog.Posts
{
public class PostResponsetDto
{
public string Id { get; set; }
public string Title { get; set; }
public string Content { get; set; }
public string Image { get; set; }
public string TopicName { get; set; }
public string Id { get; set; } = string.Empty;
public string Title { get; set; } = string.Empty;
public string Content { get; set; } = string.Empty;
public string Image { get; set; } = string.Empty;
public DateTime? CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public string TopicName { get; set; } = string.Empty;
public List<TagDto> Tags { get; set; } = new List<TagDto>();
public UserShortDto UserShort { get; set; } = new UserShortDto();
public int ViewNumber { get; set; } = 0;
Expand Down
8 changes: 4 additions & 4 deletions VNH.Application/DTOs/Catalog/Users/UserUpdateDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ namespace VNH.Application.DTOs.Catalog.Users
public class UserUpdateDto
{
[StringLength(100)]
public string Fullname { get; set; } = string.Empty;
public string? Fullname { get; set; } = string.Empty;
[Column(TypeName = "datetime")]
[DisplayFormat(DataFormatString = "{0:dd-MM-yyyy}", ApplyFormatInEditMode = true)]
public DateTime? DateOfBirth { get; set; }
public Gender Gender { get; set; }
public virtual string Email { get; set; } = string.Empty;
public virtual string PhoneNumber { get; set; } = string.Empty;
public virtual string Introduction { get; set; } = string.Empty;
public virtual string? Email { get; set; } = string.Empty;
public virtual string? PhoneNumber { get; set; } = string.Empty;
public virtual string? Introduction { get; set; } = string.Empty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@ namespace VNH.Application.Interfaces.Catalog.Accounts
public interface IAccountService
{
Task<ApiResult<string>> Authenticate(LoginRequest request);
Task<ApiResult<bool>> Register(RegisterRequest request);
Task<ApiResult<bool>> EmailConfirm(string numberConfirm, string email);
Task<ApiResult<string>> Register(RegisterRequest request);
Task<ApiResult<string>> EmailConfirm(string numberConfirm, string email);
ClaimsPrincipal ValidateToken(string jwtToken);
Task<ApiResult<LoginRequest>> ForgetPassword(string email);
Task<ApiResult<ResetPassDto>> ConfirmCode(string email);
Task<ApiResult<bool>> ResetPassword(ResetPassDto resetPass);
Task<ApiResult<string>> ResetPassword(ResetPassDto resetPass);
Task LockAccount(User user);
Task<ApiResult<bool>> ChangePassword(ChangePasswordDto changePasswodDto);
Task<ApiResult<string>> ChangePassword(ChangePasswordDto changePasswodDto);
Task<ApiResult<string>> LoginExtend(string email, string name);
Task<ApiResult<string>> ChangeEmail(string currentEmail ,string email);
}
}
2 changes: 1 addition & 1 deletion VNH.Application/Interfaces/Catalog/Posts/IPostService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public interface IPostService
Task<ApiResult<PostResponsetDto>> Update(CreatePostDto requestDto, string name);
Task<ApiResult<PostResponsetDto>> Detail(string Id);
Task<ApiResult<List<PostResponsetDto>>> GetAll();
Task<ApiResult<bool>> Delete(string id);
Task<ApiResult<string>> Delete(string id);

Task<ApiResult<string>> AddOrUnLikePost(string id, string userId);
Task<ApiResult<string>> AddOrRemoveSavePost(string postId, string userId);
Expand Down
1 change: 1 addition & 0 deletions VNH.Application/Interfaces/Common/IImageService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ namespace VNH.Application.Interfaces.Common
{
public interface IImageService
{
Task<string> SaveFile(IFormFile file);
Task<byte[]> ConvertFormFileToByteArray(IFormFile formFile);
string ConvertByteArrayToString(byte[]? byteArray, Encoding encoding);
byte[] CompressImage(byte[] originalImage, int KbNumber);
Expand Down
10 changes: 10 additions & 0 deletions VNH.Application/Interfaces/Common/IStorageService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@

namespace VNH.Application.Interfaces.Common
{
public interface IStorageService
{
string GetFileUrl(string fileName);
Task SaveFileAsync(Stream mediaBinaryStream, string fileName);
Task DeleteFileAsync(string fileName);
}
}
2 changes: 1 addition & 1 deletion VNH.Domain/Entities/Course.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public Course()
public DateTime? CreatedAt { get; set; }
[Column(TypeName = "datetime")]
public DateTime? UpdatedAt { get; set; }

public string Image { get; set; } = string.Empty;
[ForeignKey("UserId")]
[InverseProperty("Courses")]
public virtual User User { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions VNH.Domain/Entities/Document.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ public Document()
[Required]
[StringLength(255)]
public string Title { get; set; }
[StringLength(255)]
[StringLength(500)]
public string Description { get; set; }
[StringLength(255)]
[Unicode(false)]
public string UrlImage { get; set; }
public string Image { get; set; } = string.Empty;
[StringLength(255)]
[Unicode(false)]
public string FileName { get; set; }
Expand Down
4 changes: 2 additions & 2 deletions VNH.Domain/Entities/News.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ public partial class News
[StringLength(255)]
public string Title { get; set; }
[Column(TypeName = "text")]
public string Description { get; set; }
public string Description { get; set; } = string.Empty;
[Column(TypeName = "text")]
public string Image { get; set; }
public string Image { get; set; } = string.Empty;
[Column(TypeName = "datetime")]
public DateTime? PubDate { get; set; }
[Required]
Expand Down
2 changes: 1 addition & 1 deletion VNH.Domain/Entities/Post.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public Post()
[Column(TypeName = "datetime")]
public DateTime? UpdatedAt { get; set; }
public Guid? UserId { get; set; }
public byte[] Image { get; set; }
public string Image { get; set; } = string.Empty;
public int ViewNumber { get; set; }

[ForeignKey("TopicId")]
Expand Down
2 changes: 1 addition & 1 deletion VNH.Domain/Entities/User.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public User()
public DateTime? DateOfBirth { get; set; }
public string Introduction { get; set; }
public Gender Gender { get; set; }
public byte[] Image { get; set; }
public string Image { get; set; } = string.Empty;
public string NumberConfirm { get; set; } // Confirm Email
[InverseProperty("User")]
public virtual ICollection<AnswerVote> AnswerVotes { get; set; }
Expand Down
6 changes: 5 additions & 1 deletion VNH.Infrastructure/DependencyInjectionInfrastructure.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
//builder.AllowAnyOrigin()
// .AllowAnyHeader()
// .AllowAnyMethod();
});
});
services.AddOptions();
Expand All @@ -116,8 +119,9 @@ public static IServiceCollection AddInfrastructure(this IServiceCollection servi
services.Configure<MailSettings>(mailsettings);
services.AddSingleton<ISendMailService, SendMailService>();

services.AddSingleton<IImageService, ImageService>();
services.AddSingleton<IStorageService, StorageService>();

services.AddScoped<IImageService, ImageService>();
services.AddScoped<IHashTag, TagService>();
services.AddScoped<IAccountService, AccountService>();
services.AddScoped<ITopicService, TopicService>();
Expand Down
49 changes: 32 additions & 17 deletions VNH.Infrastructure/Implement/Catalog/Account/AccountService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,18 @@ public async Task<ApiResult<ResetPassDto>> ConfirmCode(string email)
return new ApiSuccessResult<ResetPassDto>(result);
}

public async Task<ApiResult<bool>> EmailConfirm(string numberConfirm, string email)
public async Task<ApiResult<string>> EmailConfirm(string numberConfirm, string email)
{
var user = await _userManager.FindByEmailAsync(email);
if (user is null) return new ApiErrorResult<bool>("Lỗi xác nhận email");
if (user is null) return new ApiErrorResult<string>("Lỗi xác nhận email");
if (user.NumberConfirm.Equals(numberConfirm))
{
user.EmailConfirmed = true;
_dataContext.User.Update(user);
await _dataContext.SaveChangesAsync();
return new ApiSuccessResult<bool>();
return new ApiSuccessResult<string>();
}
return new ApiErrorResult<bool>("Xác thực không thành công");
return new ApiErrorResult<string>("Xác thực không thành công");
}

public async Task<ApiResult<LoginRequest>> ForgetPassword(string email)
Expand Down Expand Up @@ -158,12 +158,12 @@ public async Task<ApiResult<LoginRequest>> ForgetPassword(string email)
return new ApiSuccessResult<LoginRequest>(result);
}

public async Task<ApiResult<bool>> Register(RegisterRequest request)
public async Task<ApiResult<string>> Register(RegisterRequest request)
{
try
{
var user = await _userManager.FindByEmailAsync(request.Email);
if (user != null) return new ApiErrorResult<bool>("Email đã tồn tại");
if (user != null) return new ApiErrorResult<string>("Email đã tồn tại");
var confirmNumber = GetConfirmCode();
user = new User()
{
Expand All @@ -185,16 +185,16 @@ public async Task<ApiResult<bool>> Register(RegisterRequest request)
await SendConfirmCodeToEmail(request.Email, confirmNumber);

await _userManager.AddToRoleAsync(getUser, role);
return new ApiSuccessResult<bool>();
return new ApiSuccessResult<string>();
}
}

return new ApiErrorResult<bool>("Đăng ký không thành công : Mật khẩu không hợp lệ, yêu cầu gồm có ít 6 ký tự bao gồm ký tự: Hoa, thường, số, ký tự đặc biệt ");
return new ApiErrorResult<string>("Đăng ký không thành công : Mật khẩu không hợp lệ, yêu cầu gồm có ít 6 ký tự bao gồm ký tự: Hoa, thường, số, ký tự đặc biệt ");
}
catch (Exception ex)
{
_logger.LogError("Xảy ra lỗi trong quá trình đăng ký | ", ex.Message);
return new ApiErrorResult<bool>("Lỗi đăng ký!");
return new ApiErrorResult<string>("Lỗi đăng ký!");
}
}

Expand Down Expand Up @@ -310,15 +310,15 @@ public ClaimsPrincipal ValidateToken(string jwtToken)
return principal;
}

public async Task<ApiResult<bool>> ResetPassword(ResetPassDto resetPass)
public async Task<ApiResult<string>> ResetPassword(ResetPassDto resetPass)
{
var user = await _userManager.FindByEmailAsync(resetPass.Email);
if (user is null) return new ApiErrorResult<bool>("Lỗi");
if (user is null) return new ApiErrorResult<string>("Lỗi");
var resetPasswordResult = await _userManager.ResetPasswordAsync(user, resetPass.Token, resetPass.Password);
if (!resetPasswordResult.Succeeded)
{
_logger.LogError("Xảy ra lỗi trong quá trình xử lý | ", resetPasswordResult.Errors.Select(e => e.Description));
return new ApiErrorResult<bool>("Lỗi!");
return new ApiErrorResult<string>("Lỗi!");
}
if (user.AccessFailedCount is 5)
{
Expand All @@ -327,7 +327,7 @@ public async Task<ApiResult<bool>> ResetPassword(ResetPassDto resetPass)
_dataContext.User.Update(user);
await _dataContext.SaveChangesAsync();
}
return new ApiSuccessResult<bool>();
return new ApiSuccessResult<string>();
}

public async Task LockAccount(User user)
Expand All @@ -338,20 +338,20 @@ public async Task LockAccount(User user)
await _dataContext.SaveChangesAsync();
}

public async Task<ApiResult<bool>> ChangePassword(ChangePasswordDto changePasswodDto)
public async Task<ApiResult<string>> ChangePassword(ChangePasswordDto changePasswodDto)
{
var user = await _userManager.FindByEmailAsync(changePasswodDto.Email);
var passwordCheckResult = await _userManager.CheckPasswordAsync(user, changePasswodDto.Password);
if (!passwordCheckResult)
{
return new ApiErrorResult<bool>("Mật khẩu hiện tại không đúng");
return new ApiErrorResult<string>("Mật khẩu hiện tại không đúng");
}
var changePasswordResult = await _userManager.ChangePasswordAsync(user, changePasswodDto.Password, changePasswodDto.NewPassword);
if (changePasswordResult.Succeeded)
{
return new ApiSuccessResult<bool>();
return new ApiSuccessResult<string>();
}
return new ApiErrorResult<bool>("Đổi mật khẩu không thành công");
return new ApiErrorResult<string>("Đổi mật khẩu không thành công");
}

public async Task<ApiResult<string>> LoginExtend(string email, string name)
Expand All @@ -367,5 +367,20 @@ public async Task<ApiResult<string>> LoginExtend(string email, string name)
await _signInManager.SignInAsync(user, isPersistent: false);
return new ApiSuccessResult<string>(await GetToken(user));
}

public async Task<ApiResult<string>> ChangeEmail(string currentEmail, string email)
{
var user = await _userManager.FindByEmailAsync(currentEmail);
var token = await _userManager.GenerateChangeEmailTokenAsync(user, email);
user.UserName = email;
user.NormalizedEmail = email;
user.NormalizedUserName = email;
var changeEmailResult = await _userManager.ChangeEmailAsync(user, email, token);
if (changeEmailResult.Succeeded)
{
return new ApiSuccessResult<string>( await GetToken(user));
}
return new ApiErrorResult<string>("Đổi email không thành công");
}
}
}
Loading

0 comments on commit 921203c

Please sign in to comment.