Browse Source

CommentsController fixed.

pull/330/head
Sebastian Stehle 7 years ago
parent
commit
4229fdbeb7
  1. 5
      src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs
  2. 11
      src/Squidex/Areas/Api/Controllers/Comments/CommentsController.cs
  3. 23
      src/Squidex/app/shared/services/comments.service.spec.ts
  4. 26
      src/Squidex/app/shared/services/comments.service.ts

5
src/Squidex/Areas/Api/Controllers/Assets/AssetContentController.cs

@ -10,6 +10,7 @@ using System.IO;
using System.Threading.Tasks; using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Squidex.Domain.Apps.Entities.Assets.Repositories; using Squidex.Domain.Apps.Entities.Assets.Repositories;
using Squidex.Infrastructure;
using Squidex.Infrastructure.Assets; using Squidex.Infrastructure.Assets;
using Squidex.Infrastructure.Commands; using Squidex.Infrastructure.Commands;
using Squidex.Pipeline; using Squidex.Pipeline;
@ -58,7 +59,7 @@ namespace Squidex.Areas.Api.Controllers.Assets
[Route("assets/{id}/")] [Route("assets/{id}/")]
[ProducesResponseType(200)] [ProducesResponseType(200)]
[ApiCosts(0.5)] [ApiCosts(0.5)]
public async Task<IActionResult> GetAssetContent(Guid id, [FromQuery] int version = -1, [FromQuery] int? width = null, [FromQuery] int? height = null, [FromQuery] string mode = null) public async Task<IActionResult> GetAssetContent(Guid id, [FromQuery] long version = EtagVersion.Any, [FromQuery] int? width = null, [FromQuery] int? height = null, [FromQuery] string mode = null)
{ {
var entity = await assetRepository.FindAssetAsync(id); var entity = await assetRepository.FindAssetAsync(id);
@ -67,7 +68,7 @@ namespace Squidex.Areas.Api.Controllers.Assets
return NotFound(); return NotFound();
} }
Response.Headers["ETag"] = $"{entity.FileVersion};{width};{height};{mode}"; Response.Headers["ETag"] = entity.FileVersion.ToString();
return new FileCallbackResult(entity.MimeType, entity.FileName, async bodyStream => return new FileCallbackResult(entity.MimeType, entity.FileName, async bodyStream =>
{ {

11
src/Squidex/Areas/Api/Controllers/Comments/CommentsController.cs

@ -40,6 +40,10 @@ namespace Squidex.Areas.Api.Controllers.Comments
/// </summary> /// </summary>
/// <param name="app">The name of the app.</param> /// <param name="app">The name of the app.</param>
/// <param name="commentsId">The id of the comments.</param> /// <param name="commentsId">The id of the comments.</param>
/// <param name="version">The current version.</param>
/// <remarks>
/// When passing in a version you can retrieve all updates since then.
/// </remarks>
/// <returns> /// <returns>
/// 200 => All comments returned. /// 200 => All comments returned.
/// 404 => App not found. /// 404 => App not found.
@ -48,13 +52,8 @@ namespace Squidex.Areas.Api.Controllers.Comments
[Route("apps/{app}/comments/{commentsId}")] [Route("apps/{app}/comments/{commentsId}")]
[ProducesResponseType(typeof(CommentsDto), 200)] [ProducesResponseType(typeof(CommentsDto), 200)]
[ApiCosts(0)] [ApiCosts(0)]
public async Task<IActionResult> GetComments(string app, Guid commentsId) public async Task<IActionResult> GetComments(string app, Guid commentsId, [FromQuery] long version = EtagVersion.Any)
{ {
if (!long.TryParse(Request.Headers["If-None-Match"], out var version))
{
version = EtagVersion.Any;
}
var result = await grainFactory.GetGrain<ICommentGrain>(commentsId).GetCommentsAsync(version); var result = await grainFactory.GetGrain<ICommentGrain>(commentsId).GetCommentsAsync(version);
var response = CommentsDto.FromResult(result); var response = CommentsDto.FromResult(result);

23
src/Squidex/app/shared/services/comments.service.spec.ts

@ -46,10 +46,10 @@ describe('CommentsService', () => {
comments = result; comments = result;
}); });
const req = httpMock.expectOne('http://service/p/api/apps/my-app/comments/my-comments'); const req = httpMock.expectOne('http://service/p/api/apps/my-app/comments/my-comments?version=123');
expect(req.request.method).toEqual('GET'); expect(req.request.method).toEqual('GET');
expect(req.request.headers.get('If-None-Match')).toBe('123'); expect(req.request.headers.get('If-Match')).toBeNull();
req.flush({ req.flush({
createdComments: [{ createdComments: [{
@ -81,25 +81,6 @@ describe('CommentsService', () => {
); );
})); }));
it('should make get request to get comments and return empty result for 304',
inject([CommentsService, HttpTestingController], (commentsService: CommentsService, httpMock: HttpTestingController) => {
let comments: CommentsDto;
commentsService.getComments('my-app', 'my-comments', new Version('123')).subscribe(result => {
comments = result;
});
const req = httpMock.expectOne('http://service/p/api/apps/my-app/comments/my-comments');
expect(req.request.method).toEqual('GET');
expect(req.request.headers.get('If-None-Match')).toBe('123');
req.flush({}, { status: 304, statusText: 'NotModified' });
expect(comments!).toEqual(new CommentsDto([], [], [], new Version('123')));
}));
it('should make post request to create comment', it('should make post request to create comment',
inject([CommentsService, HttpTestingController], (commentsService: CommentsService, httpMock: HttpTestingController) => { inject([CommentsService, HttpTestingController], (commentsService: CommentsService, httpMock: HttpTestingController) => {

26
src/Squidex/app/shared/services/comments.service.ts

@ -5,17 +5,16 @@
* Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved.
*/ */
import { HttpClient, HttpHeaders } from '@angular/common/http'; import { HttpClient } from '@angular/common/http';
import { Injectable } from '@angular/core'; import { Injectable } from '@angular/core';
import { Observable, of, throwError } from 'rxjs'; import { Observable } from 'rxjs';
import { catchError, map } from 'rxjs/operators'; import { map } from 'rxjs/operators';
import { import {
ApiUrlConfig, ApiUrlConfig,
DateTime, DateTime,
Model, Model,
pretifyError, pretifyError,
Types,
Version Version
} from '@app/framework'; } from '@app/framework';
@ -61,25 +60,10 @@ export class CommentsService {
} }
public getComments(appName: string, commentsId: string, version: Version): Observable<CommentsDto> { public getComments(appName: string, commentsId: string, version: Version): Observable<CommentsDto> {
const url = this.apiUrl.buildUrl(`api/apps/${appName}/comments/${commentsId}`); const url = this.apiUrl.buildUrl(`api/apps/${appName}/comments/${commentsId}?version=${version.value}`);
const options = {
headers: new HttpHeaders().set('If-None-Match', version.value)
};
return this.http.get(url, options).pipe(
catchError(err => {
if (err.status === 304) {
return of(new CommentsDto([], [], [], version));
}
return throwError(err); return this.http.get(url).pipe(
}),
map(response => { map(response => {
if (Types.is(response, CommentsDto)) {
return response;
}
const body: any = response; const body: any = response;
return new CommentsDto( return new CommentsDto(

Loading…
Cancel
Save