-
Notifications
You must be signed in to change notification settings - Fork 526
Expand file tree
/
Copy pathBlogViewModelService.cs
More file actions
246 lines (218 loc) · 10.7 KB
/
BlogViewModelService.cs
File metadata and controls
246 lines (218 loc) · 10.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
using Grand.Business.Core.Interfaces.Catalog.Products;
using Grand.Business.Core.Interfaces.Cms;
using Grand.Business.Core.Interfaces.Common.Directory;
using Grand.Business.Core.Interfaces.Common.Localization;
using Grand.Business.Core.Interfaces.Common.Seo;
using Grand.Business.Core.Interfaces.Common.Stores;
using Grand.Business.Core.Interfaces.Customers;
using Grand.Business.Core.Interfaces.Storage;
using Grand.Domain.Blogs;
using Grand.Domain.Catalog;
using Grand.Infrastructure;
using Grand.SharedKernel.Extensions;
using Grand.Web.AdminShared.Extensions;
using Grand.Web.AdminShared.Extensions.Mapping;
using Grand.Web.AdminShared.Interfaces;
using Grand.Web.AdminShared.Models.Blogs;
using Grand.Web.AdminShared.Models.Catalog;
using Grand.Web.Common.Extensions;
using Grand.Web.Common.Localization;
using Microsoft.AspNetCore.Mvc.Rendering;
namespace Grand.Web.AdminShared.Services;
public class BlogViewModelService : IBlogViewModelService
{
private readonly IBlogService _blogService;
private readonly ICustomerService _customerService;
private readonly IDateTimeService _dateTimeService;
private readonly IPictureService _pictureService;
private readonly IProductService _productService;
private readonly IStoreService _storeService;
private readonly ITranslationService _translationService;
private readonly IVendorService _vendorService;
private readonly IContextAccessor _contextAccessor;
private readonly ISeNameService _seNameService;
private readonly IEnumTranslationService _enumTranslationService;
public BlogViewModelService(
IBlogService blogService,
IDateTimeService dateTimeService,
IStoreService storeService,
IPictureService pictureService,
ICustomerService customerService,
ITranslationService translationService,
IProductService productService,
IVendorService vendorService,
IContextAccessor contextAccessor,
ISeNameService seNameService,
IEnumTranslationService enumTranslationService)
{
_blogService = blogService;
_dateTimeService = dateTimeService;
_storeService = storeService;
_pictureService = pictureService;
_customerService = customerService;
_translationService = translationService;
_productService = productService;
_vendorService = vendorService;
_contextAccessor = contextAccessor;
_seNameService = seNameService;
_enumTranslationService = enumTranslationService;
}
public virtual async Task<(IEnumerable<BlogPostModel> blogPosts, int totalCount)> PrepareBlogPostsModel(
int pageIndex, int pageSize)
{
var blogPosts = await _blogService.GetAllBlogPosts(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId, null, null,
pageIndex - 1, pageSize, true);
return (blogPosts.Select(x =>
{
var m = x.ToModel(_dateTimeService);
m.Body = "";
if (x.StartDateUtc.HasValue)
m.StartDate = _dateTimeService.ConvertToUserTime(x.StartDateUtc.Value, DateTimeKind.Utc);
if (x.EndDateUtc.HasValue)
m.EndDate = _dateTimeService.ConvertToUserTime(x.EndDateUtc.Value, DateTimeKind.Utc);
m.CreatedOn = _dateTimeService.ConvertToUserTime(x.CreatedOnUtc, DateTimeKind.Utc);
m.Comments = x.CommentCount;
return m;
}), blogPosts.TotalCount);
}
public virtual async Task<BlogPost> InsertBlogPostModel(BlogPostModel model)
{
var blogPost = model.ToEntity(_dateTimeService);
//search engine name
blogPost.Locales = await _seNameService.TranslationSeNameProperties(model.Locales, blogPost, x => x.Title);
blogPost.SeName = await _seNameService.ValidateSeName(blogPost, model.SeName, blogPost.Title, true);
await _blogService.InsertBlogPost(blogPost);
await _seNameService.SaveSeName(blogPost);
//update picture seo file name
await _pictureService.UpdatePictureSeoNames(blogPost.PictureId, blogPost.Title);
return blogPost;
}
public virtual async Task<BlogPost> UpdateBlogPostModel(BlogPostModel model, BlogPost blogPost)
{
var prevPictureId = blogPost.PictureId;
blogPost = model.ToEntity(blogPost, _dateTimeService);
//search engine name
blogPost.Locales = await _seNameService.TranslationSeNameProperties(model.Locales, blogPost, x => x.Title);
blogPost.SeName = await _seNameService.ValidateSeName(blogPost, model.SeName, blogPost.Title, true);
await _blogService.UpdateBlogPost(blogPost);
await _seNameService.SaveSeName(blogPost);
//delete an old picture (if deleted or updated)
if (!string.IsNullOrEmpty(prevPictureId) && prevPictureId != blogPost.PictureId)
{
var prevPicture = await _pictureService.GetPictureById(prevPictureId);
if (prevPicture != null)
await _pictureService.DeletePicture(prevPicture);
}
//update picture seo file name
await _pictureService.UpdatePictureSeoNames(blogPost.PictureId, blogPost.Title);
return blogPost;
}
public virtual async Task<(IEnumerable<BlogCommentModel> blogComments, int totalCount)>
PrepareBlogPostCommentsModel(string filterByBlogPostId, int pageIndex, int pageSize)
{
IList<BlogComment> comments;
var storeId = string.IsNullOrEmpty(_contextAccessor.WorkContext.CurrentCustomer.StaffStoreId)
? ""
: _contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;
if (!string.IsNullOrEmpty(filterByBlogPostId))
{
//filter comments by blog
var blogPost = await _blogService.GetBlogPostById(filterByBlogPostId);
comments = await _blogService.GetBlogCommentsByBlogPostId(blogPost.Id);
}
else
{
//load all blog comments
comments = await _blogService.GetAllComments("", storeId);
}
var commentsList = new List<BlogCommentModel>();
foreach (var blogComment in comments.Skip((pageIndex - 1) * pageSize).Take(pageSize))
{
var commentModel = new BlogCommentModel {
Id = blogComment.Id,
BlogPostId = blogComment.BlogPostId,
BlogPostTitle = blogComment.BlogPostTitle,
CustomerId = blogComment.CustomerId
};
var customer = await _customerService.GetCustomerById(blogComment.CustomerId);
commentModel.CustomerInfo = !string.IsNullOrEmpty(customer.Email)
? customer.Email
: _translationService.GetResource("Admin.Customers.Guest");
commentModel.CreatedOn = _dateTimeService.ConvertToUserTime(blogComment.CreatedOnUtc, DateTimeKind.Utc);
commentModel.Comment = FormatText.ConvertText(blogComment.CommentText);
commentsList.Add(commentModel);
}
return (commentsList, comments.Count);
}
public virtual async Task<(IEnumerable<BlogProductModel> blogProducts, int totalCount)> PrepareBlogProductsModel(
string filterByBlogPostId, int pageIndex, int pageSize)
{
var productModels = new List<BlogProductModel>();
var blogproducts = await _blogService.GetProductsByBlogPostId(filterByBlogPostId);
foreach (var item in blogproducts.Skip((pageIndex - 1) * pageSize).Take(pageSize))
productModels.Add(new BlogProductModel {
Id = item.Id,
DisplayOrder = item.DisplayOrder,
ProductId = item.ProductId,
ProductName = (await _productService.GetProductById(item.ProductId))?.Name
});
return (productModels, blogproducts.Count);
}
public virtual async Task<BlogProductModel.AddProductModel> PrepareBlogModelAddProductModel(string blogPostId)
{
var model = new BlogProductModel.AddProductModel {
BlogPostId = blogPostId
};
//stores
var storeId = _contextAccessor.WorkContext.CurrentCustomer.StaffStoreId;
model.AvailableStores.Add(new SelectListItem
{ Text = _translationService.GetResource("Admin.Common.All"), Value = " " });
foreach (var s in (await _storeService.GetAllStores()).Where(x =>
x.Id == storeId || string.IsNullOrWhiteSpace(storeId)))
model.AvailableStores.Add(new SelectListItem { Text = s.Shortcut, Value = s.Id });
//vendors
model.AvailableVendors.Add(new SelectListItem
{ Text = _translationService.GetResource("Admin.Common.All"), Value = " " });
foreach (var v in await _vendorService.GetAllVendors(showHidden: true))
model.AvailableVendors.Add(new SelectListItem { Text = v.Name, Value = v.Id });
//product types
model.AvailableProductTypes = _enumTranslationService.ToSelectList(ProductType.SimpleProduct).ToList();
model.AvailableProductTypes.Insert(0,
new SelectListItem { Text = _translationService.GetResource("Admin.Common.All"), Value = " " });
return model;
}
public virtual async Task<(IList<ProductModel> products, int totalCount)> PrepareProductModel(
BlogProductModel.AddProductModel model, int pageIndex, int pageSize)
{
var products = await _productService.PrepareProductList(model.SearchCategoryId, model.SearchBrandId,
model.SearchCollectionId, model.SearchStoreId, model.SearchVendorId, model.SearchProductTypeId,
model.SearchProductName, pageIndex, pageSize);
return (products.Select(x => x.ToModel()).ToList(), products.TotalCount);
}
public virtual async Task InsertProductModel(string blogPostId, BlogProductModel.AddProductModel model)
{
foreach (var id in model.SelectedProductIds)
{
var products = await _blogService.GetProductsByBlogPostId(blogPostId);
var product = await _productService.GetProductById(id);
if (product != null)
if (products.FirstOrDefault(x => x.ProductId == id) == null)
await _blogService.InsertBlogProduct(new BlogProduct {
BlogPostId = blogPostId,
ProductId = id,
DisplayOrder = 0
});
}
}
public virtual async Task UpdateProductModel(BlogProductModel model)
{
var bp = await _blogService.GetBlogProductById(model.Id);
bp.DisplayOrder = model.DisplayOrder;
await _blogService.UpdateBlogProduct(bp);
}
public virtual async Task DeleteProductModel(string id)
{
var bp = await _blogService.GetBlogProductById(id);
await _blogService.DeleteBlogProduct(bp);
}
}