using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using WK_KLI_LAPS_Service.App_Data;
using WK_KLI_LAPS_Service.Models.Master;

namespace WK_KLI_LAPS_Service.Controllers.Master.MipTopic2
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class MipTopic2ApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IMipTopic2Service _MipTopic2services;
        public MipTopic2ApiController(IMipTopic2Service MipTopic2Services)
        {
            _MipTopic2services = MipTopic2Services;

        }


        [Authorize]
        [HttpGet]
        //Code to retrieve all data
        public IActionResult GetAll()
        {
            var _list = _MipTopic2services.GetAll();
            return Ok(_list);
        }

        [Authorize]
        [HttpGet("GetActiveMipTopic2")]
        //Code to retrieve active data
        public IActionResult GetActiveMipTopic2()
        {
            var _list = _MipTopic2services.GetActiveMipTopic2();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] miptopic2Model omiptopic2Model)
        {

            int retval = 0;
            try
            {
                retval = _MipTopic2services.Add(omiptopic2Model);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }
        //Code to update specific record
        [Authorize]
        [HttpPost("Update")]
        public IActionResult Update([FromBody] miptopic2Model omiptopic2Model)
        {
            int retval = 0;
            try
            {
                retval = _MipTopic2services.Edit(omiptopic2Model);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }

        }


        //Code to activate or deactivate specific record
        [Authorize]
        [HttpPost("activeordeactive")]
        public IActionResult Delete([FromBody] miptopic2Model omiptopic2Model)
        {
            int retval = 0;
            try
            {
                retval = _MipTopic2services.Delete(omiptopic2Model);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}