1
1
using System ;
2
2
using System . Threading . Tasks ;
3
+ using Microsoft . AspNetCore . Identity ;
4
+ using Microsoft . Extensions . Configuration ;
3
5
using ServiceStack ;
4
6
using ServiceStack . Configuration ;
5
7
using ServiceStack . OrmLite ;
6
8
using ServiceStack . Script ;
9
+ using TechStacks . Data ;
7
10
using TechStacks . ServiceInterface . Notifications ;
8
11
using TechStacks . ServiceModel . Types ;
9
12
10
13
namespace TechStacks . ServiceInterface . Admin ;
11
14
15
+ [ ValidateIsAdmin ]
12
16
[ Route ( "/email/post/{PostId}" ) ]
13
- public class EmailTest : IReturn < EmailTestRespoonse >
17
+ public class EmailTest : IReturn < EmailTestResponse >
14
18
{
15
19
public int ? PostId { get ; set ; }
16
20
}
17
21
18
- public class EmailTestRespoonse
22
+ public class EmailTestResponse
19
23
{
20
24
public ResponseStatus ResponseStatus { get ; set ; }
21
25
}
22
26
23
- [ Authenticate ]
24
- [ RequiredRole ( "Admin" ) ]
25
- public class EmailServices : Service
27
+ public class EmailServices ( EmailProvider email , IConfiguration configuration , UserManager < ApplicationUser > userManager ) : Service
26
28
{
27
- public EmailProvider Email { get ; set ; }
28
-
29
- public IAppSettings AppSettings { get ; set ; }
30
-
31
- public async Task < EmailTestRespoonse > Any ( EmailTest request )
29
+ public async Task < EmailTestResponse > Any ( EmailTest request )
32
30
{
33
31
if ( request . PostId == null )
34
32
throw new ArgumentNullException ( nameof ( request . PostId ) ) ;
@@ -43,33 +41,34 @@ public async Task<EmailTestRespoonse> Any(EmailTest request)
43
41
var page = context . GetPage ( "emails/post-new" ) ;
44
42
var result = new PageResult ( page ) {
45
43
Args = {
46
- [ "baseUrl" ] = AppSettings . GetString ( "PublicBaseUrl" ) ,
44
+ [ "baseUrl" ] = configuration [ "PublicBaseUrl" ] ,
47
45
[ "post" ] = post ,
48
46
[ "organization" ] = org ,
49
47
}
50
48
} ;
51
49
var html = await result . RenderToStringAsync ( ) ;
52
50
53
- var user = Db . SingleById < CustomUserAuth > ( post . UserId ) ;
51
+ var user = await userManager . FindByIdAsync ( $ "{ post . UserId } ")
52
+ ?? throw HttpError . NotFound ( $ "User { post . UserId } not found") ;
54
53
55
- Email . Send ( new EmailMessage {
54
+ email . Send ( new EmailMessage {
56
55
To = new MailTo {
57
- Email = AppSettings . GetString ( "NotificationsFromEmail" ) ,
56
+ Email = configuration [ "NotificationsFromEmail" ] ,
58
57
Name = "Demis"
59
58
} ,
60
59
From = new MailTo {
61
- Email = AppSettings . GetString ( "NotificationsFromEmail" ) ,
60
+ Email = configuration [ "NotificationsFromEmail" ] ,
62
61
Name = user . DisplayName ?? user . UserName
63
62
} ,
64
63
Cc = new MailTo {
65
- Email = AppSettings . GetString ( "NotificationsCcEmail" ) ,
64
+ Email = configuration [ "NotificationsCcEmail" ] ,
66
65
Name = org . Name + " Subscribed"
67
66
} ,
68
67
Subject = $ "[{ post . Type } ] { post . Title } ",
69
68
BodyHtml = html ,
70
69
} ) ;
71
70
72
- return new EmailTestRespoonse ( ) ;
71
+ return new EmailTestResponse ( ) ;
73
72
}
74
73
75
74
}
0 commit comments