Skip to content

Commit e3a943e

Browse files
committed
1. Fixed issue with Sorting
2. Added Edit Answer Functionality
1 parent c2f0267 commit e3a943e

File tree

10 files changed

+168
-47
lines changed

10 files changed

+168
-47
lines changed

src/LetsDisc.Application/Posts/PostAppService.cs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -206,19 +206,16 @@ public async Task<PagedResultDto<PostDto>> GetQuestions(PagedAndSortedResultRequ
206206
questions = _postRepository
207207
.GetAll()
208208
.Include(a => a.CreatorUser)
209-
.Where(a => a.PostTypeId == (int)PostTypes.Question && a.Tags.Contains(tag))
210-
.Skip(input.SkipCount)
211-
.Take(input.MaxResultCount);
209+
.Where(a => a.PostTypeId == (int)PostTypes.Question && a.Tags.Contains(tag));
212210
}
213211
else
214212
{
215213
questionCount = await _postRepository.CountAsync(p => p.PostTypeId == (int)PostTypes.Question);
216214
questions = _postRepository
217215
.GetAll()
218216
.Include(a => a.CreatorUser)
219-
.Where(a => a.PostTypeId == (int)PostTypes.Question)
220-
.Skip(input.SkipCount)
221-
.Take(input.MaxResultCount);
217+
.Where(a => a.PostTypeId == (int)PostTypes.Question);
218+
222219
}
223220

224221
switch (input.Sorting)
@@ -237,7 +234,10 @@ public async Task<PagedResultDto<PostDto>> GetQuestions(PagedAndSortedResultRequ
237234
break;
238235
}
239236

240-
var questionList = await questions.ToListAsync();
237+
var questionList = await questions
238+
.Skip(input.SkipCount)
239+
.Take(input.MaxResultCount)
240+
.ToListAsync();
241241

242242
return new PagedResultDto<PostDto>
243243
{
@@ -378,6 +378,24 @@ public async Task<PostWithVoteInfo> SubmitAnswer(SubmitAnswerInput input)
378378
};
379379
}
380380

381+
public async Task<PostWithVoteInfo> UpdateAnswer(PostDto input)
382+
{
383+
CheckUpdatePermission();
384+
385+
var post = await _postRepository.GetAsync(input.Id);
386+
post.Body = input.Body;
387+
post.LastModificationTime = DateTime.Now;
388+
389+
await _postRepository.UpdateAsync(post);
390+
391+
return new PostWithVoteInfo
392+
{
393+
Post = post.MapTo<PostDto>(),
394+
Upvote = false,
395+
Downvote = false
396+
};
397+
}
398+
381399
protected override IQueryable<Post> ApplySorting(IQueryable<Post> query, PagedAndSortedResultRequestDto input)
382400
{
383401
return base.ApplySorting(query, input);

src/LetsDisc.Web.Host/src/app/posts/posts.component.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export class PostsComponent extends PagedListingComponentBase<PostDto>{
3434
}
3535
};
3636
searchString: string;
37+
sortByValue: string;
38+
defaultSortBy: string = 'newest';
3739

3840
constructor(injector: Injector, private _postService: PostServiceProxy, private titleService: Title, private route: ActivatedRoute) {
3941
super(injector);
@@ -42,7 +44,8 @@ export class PostsComponent extends PagedListingComponentBase<PostDto>{
4244

4345
ngOnChanges(changes: SimpleChanges) {
4446
if (changes['tagValue']) {
45-
this._postService.getQuestions("newest", 0, 10, this.tagValue)
47+
this.sortByValue = this.defaultSortBy;
48+
this._postService.getQuestions(this.sortByValue, 0, 10, this.tagValue)
4649
.subscribe((result: PagedResultDtoOfPostDto) => {
4750
this.questions = result.items;
4851
this.showPaging(result, 0);
@@ -53,7 +56,7 @@ export class PostsComponent extends PagedListingComponentBase<PostDto>{
5356

5457
protected list(request: PagedRequestDto, pageNumber: number, finishedCallback: Function): void {
5558
this.route.queryParamMap.subscribe(urlParams => this.searchString = urlParams.get('q'));
56-
this._postService.getQuestions(request.sorting, request.skipCount, request.maxResultCount, this.tagValue)
59+
this._postService.getQuestions(this.sortByValue, request.skipCount, request.maxResultCount, this.tagValue)
5760
.pipe(finalize(() => {
5861
finishedCallback()
5962
}))
@@ -79,6 +82,7 @@ export class PostsComponent extends PagedListingComponentBase<PostDto>{
7982
}
8083

8184
public sortBy(value: string) {
85+
this.sortByValue = value;
8286
this._postService.getQuestions(value, 0, 10, this.tagValue)
8387
.subscribe((result: PagedResultDtoOfPostDto) => {
8488
this.questions = result.items;

src/LetsDisc.Web.Host/src/app/posts/question-detail/question-detail.component.html

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ <h3>
9292
<div class="all-answers">
9393
<div class="answer-length"><h4 *ngIf="answers && answers.length > 0">{{answers.length | i18nPlural: itemPluralMapping['answer'] }}</h4></div>
9494
<div class="answer" *ngFor="let answer of answers">
95-
<div class="answer-summary" id="answer-{{answer?.post.id}}">
95+
<div class="answer-summary" id="answer-{{answer?.post.id}}" [style.display]="currentAnswerEditing?.post.id !== answer?.post.id ? 'inherit' : 'none' ">
9696
<div class="vote-layout">
9797
<a class="vote-up" (click)="voteUp(answer?.post.id)">
9898
<i [class.voted]="answer?.upvote" class="material-icons md-48">arrow_drop_up</i>
@@ -102,9 +102,21 @@ <h3>
102102
<i [class.voted]="answer?.downvote" class="material-icons md-48">arrow_drop_down</i>
103103
</a>
104104
</div>
105+
<div class="post-menu float-right">
106+
<a class="edit-post" title="edit answer" *ngIf="this.appSession.isLoggedIn && answer && this.appSession.userId === answer.post.creatorUserId" (click)="editAnswerClicked(answer)">edit</a>
107+
</div>
105108
<div class="answer-body" [innerHTML]="answer?.post.body"></div>
106109
<div class="posted-time">answered {{answer?.post.creationTime | timeAgo}} by {{answer?.post.creatorUserName}}</div>
107110
</div>
111+
<div class="edit-answer" [style.display]="answerEditing && currentAnswerEditing?.post.id === answer?.post.id ? 'inherit' : 'none'">
112+
<form #myform="ngForm" class="form answer-form" method="post">
113+
<div class="your-answer">
114+
<ckeditor id="answer-body" [(ngModel)]="currentAnswerEditing?.post.body" [editor]="Editor" [config]="config" name="Body" class="answer-body"></ckeditor>
115+
<button type="submit" class="btn btn-primary btn-save-edits" (click)="updateAnswer()">Save Edits</button>
116+
<button type="button" class="btn btn-default" (click)="answerCancelClick()">Cancel</button>
117+
</div>
118+
</form>
119+
</div>
108120
</div>
109121
</div>
110122
<form #myform="ngForm" class="form answer-form" method="post" (ngSubmit)="saveAnswer()">

src/LetsDisc.Web.Host/src/app/posts/question-detail/question-detail.component.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Component, OnInit, Injector } from '@angular/core';
2-
import { PostDto, PostServiceProxy, VoteChangeOutput, PostWithVoteInfo, SubmitAnswerInput, PostWithAnswers } from '@shared/service-proxies/service-proxies';
2+
import { PostDto, PostServiceProxy, VoteChangeOutput, PostWithVoteInfo, SubmitAnswerInput, PostWithAnswers, IPostWithVoteInfo } from '@shared/service-proxies/service-proxies';
33
import { AppComponentBase } from '@shared/app-component-base';
44
import * as ClassicEditor from '@ckeditor/ckeditor5-build-classic';
55
import { TagInputModule } from 'ngx-chips';
@@ -21,6 +21,8 @@ export class QuestionDetailComponent extends AppComponentBase implements OnInit
2121
id: number;
2222
items = [];
2323
editing: boolean = false;
24+
answerEditing: boolean = false;
25+
currentAnswerEditing: PostWithVoteInfo;
2426
saving: boolean = false;
2527
browerRefresh: boolean = false;
2628
public Editor = ClassicEditor;
@@ -130,11 +132,33 @@ export class QuestionDetailComponent extends AppComponentBase implements OnInit
130132
this.editing = true;
131133
}
132134

135+
editAnswerClicked(answer: PostWithVoteInfo): void {
136+
this.answerEditing = true;
137+
this.currentAnswerEditing = JSON.parse(JSON.stringify(answer));
138+
}
139+
133140
cancelClick(): void {
134141
this.editing = false;
135142
this.question = this.notEditedQuestion;
136143
}
137144

145+
answerCancelClick(): void {
146+
this.answerEditing = false;
147+
this.currentAnswerEditing = null;
148+
}
149+
150+
updateAnswer(): void {
151+
const index = this.answers.findIndex((item) => item.post.id == this.currentAnswerEditing.post.id);
152+
this._postService.updateAnswer(this.currentAnswerEditing.post)
153+
.subscribe((result: PostWithVoteInfo) => {
154+
this.answers[index] = result;
155+
this.answers = JSON.parse(JSON.stringify(this.answers));
156+
this.notify.info('Answer Updated');
157+
this.answerEditing = false;
158+
this.currentAnswerEditing = null;
159+
});
160+
}
161+
138162
saveQuestion(): void {
139163
let localItems = [];
140164
this.items.forEach(function (item) {

src/LetsDisc.Web.Host/src/app/search-posts/search-posts.component.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export class SearchPostsComponent extends PagedListingComponentBase<PostDto>{
2626
}
2727
};
2828
searchString: string;
29+
sortByValue: string;
2930
defaultSortBy: string = 'newest';
3031

3132
constructor(injector: Injector, private _postService: PostServiceProxy, private titleService: Title, private route: ActivatedRoute, private router: Router) {
@@ -35,7 +36,7 @@ export class SearchPostsComponent extends PagedListingComponentBase<PostDto>{
3536
protected list(request: PagedRequestDto, pageNumber: number, finishedCallback: Function): void {
3637
this.route.queryParamMap.subscribe(urlParams => this.searchString = urlParams.get('q'));
3738
if (this.searchString !== '') {
38-
this._postService.getSearchPosts(request.sorting, request.skipCount, request.maxResultCount, this.searchString)
39+
this._postService.getSearchPosts(this.sortByValue, request.skipCount, request.maxResultCount, this.searchString)
3940
.pipe(finalize(() => {
4041
finishedCallback()
4142
}))
@@ -55,6 +56,7 @@ export class SearchPostsComponent extends PagedListingComponentBase<PostDto>{
5556
}
5657

5758
public sortBy(value: string): void {
59+
this.sortByValue = value;
5860
if (this.searchString !== '') {
5961
this._postService.getSearchPosts(value, 0, 10, this.searchString)
6062
.subscribe((result: PagedResultDtoOfPostDto) => {
@@ -76,7 +78,7 @@ export class SearchPostsComponent extends PagedListingComponentBase<PostDto>{
7678
queryParams: { q: this.searchString },
7779
queryParamsHandling: 'merge'
7880
});
79-
this._postService.getSearchPosts(this.defaultSortBy, 0, 10, this.searchString)
81+
this._postService.getSearchPosts(this.sortByValue, 0, 10, this.searchString)
8082
.subscribe((result: PagedResultDtoOfPostDto) => {
8183
this.posts = result.items;
8284
this.showPaging(result, 0);

src/LetsDisc.Web.Host/src/app/tags/tags.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export class TagsComponent extends PagedListingComponentBase<TagDto> {
2222
}
2323

2424
protected list(request: PagedRequestDto, pageNumber: number, finishedCallback: Function): void {
25-
this._tagService.getTags(request.maxResultCount, request.skipCount)
25+
this._tagService.getTags(request.skipCount, request.maxResultCount)
2626
.pipe(finalize(() => {
2727
finishedCallback()
2828
}))

src/LetsDisc.Web.Host/src/app/users/user-detail/user-detail.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<div class="user-reputation">{{userDetails?.reputation}} Reputation</div>
88
<div class="user-questions"><strong>{{questionsCount}}</strong>{{questionsCount | i18nPlural: itemPluralMapping['question']}}</div>
99
<div class="user-answers"><strong>{{answersCount}}</strong>{{answersCount | i18nPlural: itemPluralMapping['answer']}}</div>
10-
<div class="user-views">{{userDetails?.views | i18nPlural: itemPluralMapping['view']}}</div>
10+
<div class="user-views">{{userDetails?.views | i18nPlural: itemPluralMapping['profileview']}}</div>
1111
<div class="user-member-for">Member for {{ createdTimeAgo }}</div>
1212

1313
</div>

src/LetsDisc.Web.Host/src/app/users/user-detail/user-detail.component.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,16 @@ export class UserDetailComponent extends AppComponentBase implements OnInit {
4040
'=1': ' answer',
4141
'other': ' answers'
4242
},
43-
'view': {
43+
'profileview': {
4444
'=0': '0 profile views',
4545
'=1': '1 profile view',
4646
'other': '# profile views'
4747
},
48+
'view': {
49+
'=0': '0 views',
50+
'=1': '1 view',
51+
'other': '# views'
52+
},
4853
'question': {
4954
'=0': ' questions',
5055
'=1': ' question',
@@ -96,7 +101,7 @@ export class UserDetailComponent extends AppComponentBase implements OnInit {
96101
}
97102

98103
protected questionList(userId: number, request: PagedRequestDto, pageNumber: number, finishedCallback: Function): void {
99-
this._userService.getUserQuestions(request.maxResultCount, request.skipCount, userId)
104+
this._userService.getUserQuestions(request.skipCount, request.maxResultCount, userId)
100105
.pipe(finalize(() => {
101106
finishedCallback()
102107
}))
@@ -107,7 +112,7 @@ export class UserDetailComponent extends AppComponentBase implements OnInit {
107112
}
108113

109114
protected answerList(userId: number, request: PagedRequestDto, pageNumber: number, finishedCallback: Function): void {
110-
this._userService.getUserAnswers(request.maxResultCount, request.skipCount, userId)
115+
this._userService.getUserAnswers(request.skipCount, request.maxResultCount, userId)
111116
.pipe(finalize(() => {
112117
finishedCallback()
113118
}))

src/LetsDisc.Web.Host/src/app/users/users.component.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class UsersComponent extends PagedListingComponentBase<UserDto> {
2929
}
3030

3131
protected list(request: PagedRequestDto, pageNumber: number, finishedCallback: Function): void {
32-
this._userService.getALLUsers(request.maxResultCount, request.skipCount)
32+
this._userService.getALLUsers(request.skipCount, request.maxResultCount)
3333
.pipe(finalize(() => {
3434
finishedCallback()
3535
}))

0 commit comments

Comments
 (0)