Skip to content
Snippets Groups Projects
monthlyNews.service.ts 2.16 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guilhem CARRON's avatar
    Guilhem CARRON committed
    import axios from 'axios'
    import https from 'https'
    export class MonthlyNewsService {
      private readonly _apiUrl: string
      constructor() {
        this._apiUrl = 'https://localhost:1443/'
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
    
       * Creates a quotation and header for selected month
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
       * @param date
       * @param header
       * @param quote
       */
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      public createMonthlyReport = async (
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        header: string,
        quote: string
      ): Promise<void> => {
        try {
          const response = await fetch(
            'https://localhost:1443/api/admin/monthlyNews',
            {
              method: 'POST',
              body: JSON.stringify({
    
                month: date.getMonth(),
                year: date.getFullYear(),
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
                header: header,
                quote: quote,
              }),
            }
          )
          if (response.status !== 201) {
            throw new Error(
              `Le post n'a pas pu être créé (code ${response.status})`
            )
          }
          console.log('Le post a été créé avec succès')
        } catch (e) {
          console.log(e)
        }
      }
    
    
      /**
       * Gets a quotation and header for selected month
       */
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      public getSingleMonthlyReport = async (
        year: string,
        month: string
      ): Promise<any> => {
    
        try {
          const response = await fetch(
            'https://localhost:1443/api/admin/monthlyNews',
            {
              method: 'GET',
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
              body: JSON.stringify({
                year: year,
                month: month,
              }),
    
            }
          )
          if (response.status !== 201) {
            throw new Error(
              `Erreur lors de la récupération (code ${response.status})`
            )
          }
          return response.json()
        } catch (e) {
          console.log(e)
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      /**
       * apiAuth  */
      public apiAuth = async (): Promise<any> => {
        try {
          axios.get(`${this._apiUrl}OAuth2Login`).finally(async () => {
            const { data } = await axios.get(`${this._apiUrl}api/common/WhoAmI`)
            console.log(data)
          })
        } catch (e) {
          console.log(e)
        }
      }
    
      /**
       * getToken
       */
      public getToken = async (): Promise<any> => {
        try {
          const response = await axios.get(`${this._apiUrl}api/common/WhoAmI`)
          console.log(response)
        } catch (e) {
          console.log(e)
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    }