Skip to content

Commit 1f7a9d8

Browse files
Merge pull request #16 from YunusOzdemirr/Yunus
UserPicture Dtos and Manager refactored
2 parents 6c19024 + 75fe49e commit 1f7a9d8

File tree

10 files changed

+137
-83
lines changed

10 files changed

+137
-83
lines changed

src/v2/CmnSoftwareBackend.Entities/CmnSoftwareBackend.Entities.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
<Folder Include="Dtos\EmailDtos\" />
3434
<Folder Include="Dtos\OperationClaimDtos\" />
3535
<Folder Include="Dtos\UserNotificationDtos\" />
36-
<Folder Include="Dtos\UserPictureUpdateDtos\" />
36+
<Folder Include="Dtos\UserPictureDtos\" />
3737
<Folder Include="Dtos\UserOperationClaimDtos\" />
3838
<Folder Include="Dtos\CategoryDtos\" />
3939
<Folder Include="Dtos\LandingEmailDtos\" />

src/v2/CmnSoftwareBackend.Entities/Dtos/UserDtos/UserDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Collections.Generic;
33
using CmnSoftwareBackend.Entities.ComplexTypes;
44
using CmnSoftwareBackend.Entities.Dtos.UserNotificationDtos;
5-
using CmnSoftwareBackend.Entities.Dtos.UserPictureUpdateDtos;
5+
using CmnSoftwareBackend.Entities.Dtos.UserPictureDtos;
66

77
namespace CmnSoftwareBackend.Entities.Dtos.UserDtos
88
{

src/v2/CmnSoftwareBackend.Entities/Dtos/UserPictureUpdateDtos/UserPictureAddDto.cs renamed to src/v2/CmnSoftwareBackend.Entities/Dtos/UserPictureDtos/UserPictureAddDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.ComponentModel;
33
using System.ComponentModel.DataAnnotations;
44

5-
namespace CmnSoftwareBackend.Entities.Dtos.UserPictureUpdateDtos
5+
namespace CmnSoftwareBackend.Entities.Dtos.UserPictureDtos
66
{
77
public class UserPictureAddDto
88
{

src/v2/CmnSoftwareBackend.Entities/Dtos/UserPictureUpdateDtos/UserPictureAddWithUserDto.cs renamed to src/v2/CmnSoftwareBackend.Entities/Dtos/UserPictureDtos/UserPictureAddWithUserDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.ComponentModel;
33
using System.ComponentModel.DataAnnotations;
44

5-
namespace CmnSoftwareBackend.Entities.Dtos.UserPictureUpdateDtos
5+
namespace CmnSoftwareBackend.Entities.Dtos.UserPictureDtos
66
{
77
public class UserPictureAddWithUserDto
88
{

src/v2/CmnSoftwareBackend.Entities/Dtos/UserPictureUpdateDtos/UserPictureDto.cs renamed to src/v2/CmnSoftwareBackend.Entities/Dtos/UserPictureDtos/UserPictureDto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
using System;
22
using CmnSoftwareBackend.Entities.Dtos.UserDtos;
33

4-
namespace CmnSoftwareBackend.Entities.Dtos.UserPictureUpdateDtos
4+
namespace CmnSoftwareBackend.Entities.Dtos.UserPictureDtos
55
{
66
public class UserPictureDto
77
{
8-
public Guid Id { get; set; }
8+
public int Id { get; set; }
99
public Guid UserId { get; set; }
1010
public UserDto User { get; set; }
1111
public string File { get; set; }
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using CmnSoftwareBackend.Entities.Concrete;
4+
using CmnSoftwareBackend.Shared.Entities.Abstract;
5+
6+
namespace CmnSoftwareBackend.Entities.Dtos.UserPictureDtos
7+
{
8+
public class UserPictureListDto:DtoGetBase
9+
{
10+
public IEnumerable<UserPicture> UserPictures{ get; set; }
11+
}
12+
}
13+

src/v2/CmnSoftwareBackend.Entities/Dtos/UserPictureUpdateDtos/UserPictureUpdateDto.cs renamed to src/v2/CmnSoftwareBackend.Entities/Dtos/UserPictureDtos/UserPictureUpdateDto.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
using System.ComponentModel;
33
using System.ComponentModel.DataAnnotations;
44

5-
namespace CmnSoftwareBackend.Entities.Dtos.UserPictureUpdateDtos
5+
namespace CmnSoftwareBackend.Entities.Dtos.UserPictureDtos
66
{
77
public class UserPictureUpdateDto
88
{
99
[DisplayName("Kullanıcının Resim Kodu")]
1010
[Required(ErrorMessage = "{0} alanı boş geçilemez.")]
11-
public Guid Id { get; set; }
11+
public int Id { get; set; }
1212

1313
[DisplayName("Kullanıcı Kodu")]
1414
[Required(ErrorMessage = "{0} alanı boş geçilemez.")]

src/v2/CmnSoftwareBackend.Services/Abstract/IUserPictureService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using System;
22
using System.Threading.Tasks;
33
using CmnSoftwareBackend.Entities.ComplexTypes;
4-
using CmnSoftwareBackend.Entities.Dtos.UserPictureUpdateDtos;
4+
using CmnSoftwareBackend.Entities.Dtos.UserPictureDtos;
55
using CmnSoftwareBackend.Shared.Utilities.Results.Abstract;
66

77
namespace CmnSoftwareBackend.Services.Abstract
88
{
99
public interface IUserPictureService
1010
{
11-
Task<IDataResult> GetAllAsync(bool? isActive, bool? isDeleted, bool isAscending, int currentPage, int pageSize, OrderBy orderBy);
11+
Task<IDataResult> GetAllAsync(bool? isActive, bool? isDeleted, bool isAscending, int currentPage, int pageSize, OrderBy orderBy,bool includeUser);
1212
Task<IDataResult> GetByIdAsync(int userPictureId);
1313
Task<IDataResult> AddAsync(UserPictureAddDto userPictureAddDto);
1414
Task<IDataResult> UpdateAsync(UserPictureUpdateDto userPictureUpdateDto);

src/v2/CmnSoftwareBackend.Services/Concrete/UserAuthManager.cs

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -41,49 +41,46 @@ public async Task<IDataResult> ActiveEmailByActivationCodeAsync(UserEmailActivat
4141
ValidationTool.Validate(new UserEmailActivateDtoValidator(), userEmailActivateDto);
4242

4343
var user = await DbContext.Users.AsNoTracking().FirstOrDefaultAsync(u => u.EmailAddress == userEmailActivateDto.EmailAddress);
44-
if (user != null)
45-
{
46-
if (string.Equals(userEmailActivateDto.ActivationCode, user.VerificationCode))
47-
{
48-
user.IsEmailAddressVerified = true;
49-
user.ModifiedDate = DateTime.Now;
50-
var accessToken = CreateAccessToken(user);
51-
user.LastLogin = DateTime.Now;
52-
UserToken userToken = new UserToken
53-
{
54-
UserId = user.Id,
55-
Token = accessToken.Token,
56-
TokenExpiration = accessToken.TokenExpiration,
57-
RefreshToken = accessToken.RefreshToken,
58-
RefreshTokenExpiration = accessToken.RefreshTokenExpiration,
59-
CreatedByUserId = user.Id,
60-
CreatedDate = DateTime.Now,
61-
ModifiedByUserId = null,
62-
ModifiedDate = null,
63-
IpAddress = userEmailActivateDto.IpAddress,
64-
IsActive = true,
65-
IsDeleted = false
66-
};
67-
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
68-
{
69-
DbContext.Users.Update(user);
70-
await DbContext.UserTokens.AddAsync(userToken);
71-
await DbContext.SaveChangesAsync();
72-
transactionScope.Complete();
73-
}
74-
return new DataResult(ResultStatus.Success,
75-
$"{user.EmailAddress} e-posta adresi başarıyla onaylanmıştır.", new UserWithTokenDto
76-
{
77-
User = Mapper.Map<UserDto>(user),
78-
UserToken = Mapper.Map<AccessToken>(userToken)
79-
});
80-
}
44+
if (user == null)
45+
throw new NotFoundArgumentException(Messages.General.ValidationError(), new Error($"{userEmailActivateDto.EmailAddress} e-posta adresine ait bir kullanıcı bulunamadı",
46+
"EmailAddress"));
47+
if (!string.Equals(userEmailActivateDto.ActivationCode, user.VerificationCode))
8148
throw new NotFoundArgumentException(Messages.General.ValidationError(),
8249
new Error($"{userEmailActivateDto.ActivationCode} numaralı aktivasyon kodu doğru değildir.", "ActivationCode"));
8350

51+
user.IsEmailAddressVerified = true;
52+
user.ModifiedDate = DateTime.Now;
53+
var accessToken = CreateAccessToken(user);
54+
user.LastLogin = DateTime.Now;
55+
UserToken userToken = new UserToken
56+
{
57+
UserId = user.Id,
58+
Token = accessToken.Token,
59+
TokenExpiration = accessToken.TokenExpiration,
60+
RefreshToken = accessToken.RefreshToken,
61+
RefreshTokenExpiration = accessToken.RefreshTokenExpiration,
62+
CreatedByUserId = user.Id,
63+
CreatedDate = DateTime.Now,
64+
ModifiedByUserId = null,
65+
ModifiedDate = null,
66+
IpAddress = userEmailActivateDto.IpAddress,
67+
IsActive = true,
68+
IsDeleted = false
69+
};
70+
using (TransactionScope transactionScope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
71+
{
72+
DbContext.Users.Update(user);
73+
await DbContext.UserTokens.AddAsync(userToken);
74+
await DbContext.SaveChangesAsync();
75+
transactionScope.Complete();
8476
}
85-
throw new NotFoundArgumentException(Messages.General.ValidationError(), new Error($"{userEmailActivateDto.EmailAddress} e-posta adresine ait bir kullanıcı bulunamadı",
86-
"EmailAddress"));
77+
return new DataResult(ResultStatus.Success,
78+
$"{user.EmailAddress} e-posta adresi başarıyla onaylanmıştır.", new UserWithTokenDto
79+
{
80+
User = Mapper.Map<UserDto>(user),
81+
UserToken = Mapper.Map<AccessToken>(userToken)
82+
});
83+
8784
}
8885

8986
public AccessToken CreateAccessToken(User user)
@@ -99,32 +96,31 @@ public async Task<IDataResult> ForgotPasswordAsync(string emailAddress)
9996
throw new NotFoundArgumentException(Messages.General.ValidationError(), new Error("Mail adresi geçerli değil", "emailAddress"));
10097

10198
var user = await DbContext.Users.FirstOrDefaultAsync(u => u.EmailAddress == emailAddress);
102-
if (user != null)
103-
{
104-
//create random password
105-
Random random = new Random();
106-
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
107-
string newPassword = new string(Enumerable.Repeat(chars, 6)
108-
.Select(s => s[random.Next(s.Length)]).ToArray());
109-
//user update
110-
byte[] passwordHash, passwordSalt;
111-
HashingHelper.CreatePasswordHash(newPassword, out passwordHash, out passwordSalt);
112-
user.PasswordHash = passwordHash;
113-
user.PasswordSalt = passwordSalt;
114-
user.ModifiedDate = DateTime.Now;
115-
DbContext.Update(user);
116-
await DbContext.SaveChangesAsync();
117-
//send new password to email
118-
_mailService.SendEmaiL(new EmailSendDto
119-
{
120-
EmailAdress = user.EmailAddress,
121-
Subject = "Şifremi Unuttum",
122-
Content = $"<h5>Hesabınızda Şifre Yenileme Aksiyonu!</h5><br/><h5>Yeni şifreniz: {newPassword} </h5>"
123-
});
124-
return new DataResult(ResultStatus.Success,
125-
$"{user.EmailAddress} e-posta adresine yeni şifreniz gönderilmiştir.", Mapper.Map<UserDto>(user));
126-
}
99+
if (user == null)
127100
throw new NotFoundArgumentException(Messages.General.ValidationError(), new Error("Böyle bir mail adresi bulunumamakta", "emailAddress"));
101+
102+
//create random password
103+
Random random = new Random();
104+
const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
105+
string newPassword = new string(Enumerable.Repeat(chars, 6)
106+
.Select(s => s[random.Next(s.Length)]).ToArray());
107+
//user update
108+
byte[] passwordHash, passwordSalt;
109+
HashingHelper.CreatePasswordHash(newPassword, out passwordHash, out passwordSalt);
110+
user.PasswordHash = passwordHash;
111+
user.PasswordSalt = passwordSalt;
112+
user.ModifiedDate = DateTime.Now;
113+
DbContext.Update(user);
114+
await DbContext.SaveChangesAsync();
115+
//send new password to email
116+
_mailService.SendEmaiL(new EmailSendDto
117+
{
118+
EmailAdress = user.EmailAddress,
119+
Subject = "Şifremi Unuttum",
120+
Content = $"<h5>Hesabınızda Şifre Yenileme Aksiyonu!</h5><br/><h5>Yeni şifreniz: {newPassword} </h5>"
121+
});
122+
return new DataResult(ResultStatus.Success,
123+
$"{user.EmailAddress} e-posta adresine yeni şifreniz gönderilmiştir.", Mapper.Map<UserDto>(user));
128124
}
129125

130126
public IEnumerable<OperationClaim> GetClaims(User user)

src/v2/CmnSoftwareBackend.Services/Concrete/UserPictureManager.cs

Lines changed: 54 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
using System;
2+
using System.Linq;
23
using System.Threading.Tasks;
34
using AutoMapper;
45
using CmnSoftwareBackend.Data.Concrete.EntityFramework.Contexts;
56
using CmnSoftwareBackend.Entities.ComplexTypes;
67
using CmnSoftwareBackend.Entities.Concrete;
7-
using CmnSoftwareBackend.Entities.Dtos.UserPictureUpdateDtos;
8+
using CmnSoftwareBackend.Entities.Dtos.UserPictureDtos;
89
using CmnSoftwareBackend.Services.Abstract;
910
using CmnSoftwareBackend.Services.Utilities;
1011
using CmnSoftwareBackend.Shared.Entities.Concrete;
@@ -49,24 +50,68 @@ public async Task<IDataResult> DeleteAsync(int userPictureId)
4950
return new DataResult(ResultStatus.Success, picture);
5051
}
5152

52-
public Task<IDataResult> GetAllAsync(bool? isActive, bool? isDeleted, bool isAscending, int currentPage, int pageSize, OrderBy orderBy)
53+
public async Task<IDataResult> GetAllAsync(bool? isActive, bool? isDeleted, bool isAscending, int currentPage, int pageSize, OrderBy orderBy, bool includeUser)
5354
{
54-
throw new NotImplementedException();
55+
IQueryable<UserPicture> query = DbContext.Set<UserPicture>();
56+
if (isActive.HasValue) query = query.AsNoTracking().Where(a => a.IsActive == isActive);
57+
if (isDeleted.HasValue) query = query.AsNoTracking().Where(a => a.IsDeleted == isDeleted);
58+
if (includeUser) query = query.AsNoTracking().Include(a => a.User);
59+
60+
pageSize = pageSize > 100 ? 100 : pageSize;
61+
var totalCount =await query.AsNoTracking().CountAsync();
62+
switch (orderBy)
63+
{
64+
case OrderBy.Id:
65+
query = isAscending ? query.OrderBy(a => a.Id) : query.OrderByDescending(a => a.Id);
66+
break;
67+
default:
68+
query = isAscending ? query.OrderBy(a => a.CreatedDate) : query.OrderByDescending(a => a.CreatedDate);
69+
break;
70+
}
71+
return new DataResult(ResultStatus.Success, new UserPictureListDto
72+
{
73+
// Articles = await query.Skip((currentPage - 1) * pageSize).Take(pageSize).Select(a => Mapper.Map<Article>(a)).ToListAsync(),
74+
UserPictures =await query.Take((currentPage - 1) * pageSize).Take(pageSize).Select(a => Mapper.Map<UserPicture>(a)).ToListAsync(),
75+
IsAscending=isAscending,
76+
CurrentPage=currentPage,
77+
PageSize=pageSize,
78+
TotalCount=totalCount
79+
});
80+
5581
}
5682

57-
public Task<IDataResult> GetByIdAsync(int userPictureId)
83+
public async Task<IDataResult> GetByIdAsync(int userPictureId)
5884
{
59-
throw new NotImplementedException();
85+
IQueryable<UserPicture> query = DbContext.Set<UserPicture>();
86+
var userPicture = await query.AsNoTracking().Include(ab=>ab.User).SingleOrDefaultAsync(a => a.Id == userPictureId);
87+
if (userPicture is null)
88+
throw new NotFoundArgumentException(Messages.General.ValidationError(), new Error("Böyle bir resim bulunamadı", "userPictureId"));
89+
90+
return new DataResult(ResultStatus.Success, query);
6091
}
6192

62-
public Task<IDataResult> HardDeleteAsync(int userPictureId)
93+
public async Task<IDataResult> HardDeleteAsync(int userPictureId)
6394
{
64-
throw new NotImplementedException();
95+
var userPicture = DbContext.UserPictures.SingleOrDefaultAsync(a=>a.Id==userPictureId);
96+
if (userPicture is null)
97+
throw new NotFoundArgumentException(Messages.General.ValidationError(), new Error("Böyle bir resim bulunamadı","userPictureId"));
98+
99+
DbContext.Remove(userPicture);
100+
await DbContext.SaveChangesAsync();
101+
return new DataResult(ResultStatus.Success, userPicture);
65102
}
66103

67-
public Task<IDataResult> UpdateAsync(UserPictureUpdateDto userPictureUpdateDto)
104+
public async Task<IDataResult> UpdateAsync(UserPictureUpdateDto userPictureUpdateDto)
68105
{
69-
throw new NotImplementedException();
106+
107+
if (!await DbContext.UserPictures.AnyAsync(a => a.Id == userPictureUpdateDto.Id))
108+
throw new NotFoundArgumentException(Messages.General.ValidationError(),new Error("Böyle bir resim bulunamadı","id"));
109+
110+
var newUserPicture = Mapper.Map<UserPicture>(userPictureUpdateDto);
111+
newUserPicture.ModifiedDate = DateTime.Now;
112+
DbContext.UserPictures.Update(newUserPicture);
113+
await DbContext.SaveChangesAsync();
114+
return new DataResult(ResultStatus.Success, newUserPicture);
70115
}
71116
}
72117
}

0 commit comments

Comments
 (0)