Skip to content
Snippets Groups Projects
monthlyNews.service.ts 2.62 KiB
Newer Older
  • Learn to ignore specific revisions
  • Guilhem CARRON's avatar
    Guilhem CARRON committed
    import axios from 'axios'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { IMonthlyNews } from '../models/monthlyNews.model'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    import { IPoll } from '../models/poll.model'
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    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,
        token: string
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      ): Promise<void> => {
        try {
    
          await axios.post(
            `${this._apiUrl}api/admin/monthlyNews`,
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            {
    
              month: date.getMonth(),
              year: date.getFullYear(),
              header: header,
              quote: quote,
            },
            {
              headers: {
                'XSRF-TOKEN': token,
              },
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
            }
          )
        } catch (e) {
          console.log(e)
        }
      }
    
    
      /**
       * Gets a quotation and header for selected month
       */
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      public getSingleMonthlyReport = async (
    
        year: number,
        month: number,
        token: string
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
      ): Promise<IMonthlyNews | null> => {
    
          const { data } = await axios.get(
            `${this._apiUrl}api/admin/monthlyNews/${year}/${month}`,
    
              headers: {
                'XSRF-TOKEN': token,
              },
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          console.log('monthlyFetched', data)
          if (data == {}) {
            return null
          }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          return data as IMonthlyNews
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        } catch (e) {
    
          console.log('error', e)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
          return null
        }
      }
    
      /**
       * Gets a poll with question and link for selected month
       */
      public getSinglePoll = async (
        year: number,
        month: number,
        token: string
      ): Promise<IPoll | null> => {
        try {
          const { data } = await axios.get(
            `${this._apiUrl}api/admin/poll/${year}/${month}`,
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
          if (data === {}) {
            return null
          }
          return data as IPoll
        } catch (e) {
          console.log('error', e)
          return null
        }
      }
    
      /**
       * Creates a poll with question and link for selected month
       * @param date
       * @param question
       * @param link
       */
      public createPoll = async (
        date: Date,
        question: string,
        link: string,
        token: string
      ): Promise<void> => {
        try {
          await axios.post(
            `${this._apiUrl}api/admin/poll`,
            {
              month: date.getMonth(),
              year: date.getFullYear(),
              link: link,
              question: question,
            },
            {
              headers: {
                'XSRF-TOKEN': token,
              },
            }
          )
        } catch (e) {
          console.log(e)
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
        }
      }
    
    Guilhem CARRON's avatar
    Guilhem CARRON committed
    }