<commit_before>import { Injectable } from '@angular/core';

import { Attachment } from './attachment.model';
import { CmsApiService } from '../../../core/api/cms-api.service';

/**
 * This service implements API calls related to attachments.
 */
@Injectable()
export class AttachmentService {

  constructor(private cmsApiService: CmsApiService) {}

  public createAttachment(attachment: Attachment, fileToUpload: any) {
    let fd = new FormData();
    fd.append('AttachmentName', attachment.name);
    fd.append('Description', attachment.description);
    fd.append('file', fileToUpload);

    let data = {
      attatchmentName: attachment.name,
      description: attachment.description
    };
    return this.cmsApiService.postUrl('/Api/Topics/' + attachment.topicId + '/Attachments', JSON.stringify(data), {})
      .toPromise()
      .then(
        (response: any) => this.addFile(response.json().value, attachment.topicId, fd)
      ).catch(
        (error: any) => this.handleError(error)
      );
  }

  private addFile(attachmentId: number, topicId: number, fd: FormData) {
    return this.cmsApiService.putUrlWithFormData('/Api/Topics/' + topicId + '/Attachments/' + attachmentId, fd)
      .toPromise()
      .then(
        (res: any) => {
          return res;
        }
      ).catch(
        (error: any) => this.handleError(error)
      );
  }

  public deleteAttachment(id: number, topicId: number) {
    return this.cmsApiService.deleteUrl('/Api/Topics/' + topicId + '/Attachments/' + id, {})
      .toPromise()
      .then(
        (response: any) => response
      ).catch(
        (error: any) => this.handleError(error)
      );
  }

  public getAttachment(id: number, topicId: number) {
    return this.cmsApiService.getUrl('/Api/Topics/' + topicId + '/Attachments/' + id, {})
      .toPromise()
      .then(
        (response: any) => {
          let hash = response.json().value;
          return this.cmsApiService.getRoot() + '/Download/' + hash;
        }
      ).catch(
        (error: any) => this.handleError(error)
      );
  }

  public getAllAttachmentsOfTopic(topicId: number) {
    return this.cmsApiService.getUrl('/api/Topics/' + topicId + '/Attachments', {})
      .toPromise()
      .then(
        (response: any) => Attachment.extractArrayData(response, topicId)
      ).catch(<commit_msg>Bugfix: Wrong API call for download attachments<commit_after>import { Injectable } from '@angular/core';

import { Attachment } from './attachment.model';
import { CmsApiService } from '../../../core/api/cms-api.service';

/**
 * This service implements API calls related to attachments.
 */
@Injectable()
export class AttachmentService {

  constructor(private cmsApiService: CmsApiService) {}

  public createAttachment(attachment: Attachment, fileToUpload: any) {
    let fd = new FormData();
    fd.append('AttachmentName', attachment.name);
    fd.append('Description', attachment.description);
    fd.append('file', fileToUpload);

    let data = {
      attatchmentName: attachment.name,
      description: attachment.description
    };
    return this.cmsApiService.postUrl('/Api/Topics/' + attachment.topicId + '/Attachments', JSON.stringify(data), {})
      .toPromise()
      .then(
        (response: any) => this.addFile(response.json().value, attachment.topicId, fd)
      ).catch(
        (error: any) => this.handleError(error)
      );
  }

  private addFile(attachmentId: number, topicId: number, fd: FormData) {
    return this.cmsApiService.putUrlWithFormData('/Api/Topics/' + topicId + '/Attachments/' + attachmentId, fd)
      .toPromise()
      .then(
        (res: any) => {
          return res;
        }
      ).catch(
        (error: any) => this.handleError(error)
      );
  }

  public deleteAttachment(id: number, topicId: number) {
    return this.cmsApiService.deleteUrl('/Api/Topics/' + topicId + '/Attachments/' + id, {})
      .toPromise()
      .then(
        (response: any) => response
      ).catch(
        (error: any) => this.handleError(error)
      );
  }

  public getAttachment(id: number, topicId: number) {
    return this.cmsApiService.getUrl('/Api/Topics/' + topicId + '/Attachments/' + id, {})
      .toPromise()
      .then(
        (response: any) => {
          let hash = response.json().value;
          return this.cmsApiService.getRoot() + '/Api/Download/' + hash;
        }
      ).catch(
        (error: any) => this.handleError(error)
      );
  }

  public getAllAttachmentsOfTopic(topicId: number) {
    return this.cmsApiService.getUrl('/api/Topics/' + topicId + '/Attachments', {})
      .toPromise()
      .then(
        (response: any) => Attachment.extractArrayData(response, topicId)
      ).catch(
{'fraction_non_alphanumeric': 0.1523404255319149, 'fraction_numerical': 0.0, 'mean_word_length': 2.7911290322580644, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3873041', 'n_tokens_mistral': 1432, 'n_tokens_neox': 1419, 'n_words': 295}