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.MipTopic1
{
    [EnableCors("CorsPolicy")]
    [Authorize]
    [ApiController]
    [Route("api/[controller]")]
    public class MipTopic1ApiController : ControllerBase
    {
        CommonRepository oCommonRepository = new CommonRepository();

        private IMipTopic1Service _MipTopic1services;
        public MipTopic1ApiController(IMipTopic1Service MipTopic1Services)
        {
            _MipTopic1services = MipTopic1Services;

        }


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

        [Authorize]
        [HttpGet("GetActiveMipTopic1")]
        //Code to retrieve active data
        public IActionResult GetActiveMipTopic1()
        {
            var _list = _MipTopic1services.GetActiveMipTopic1();
            return Ok(_list);
        }
        //Code to add new record
        [Authorize]
        [HttpPost("Add")]
        public IActionResult Add([FromBody] miptopic1Model omiptopic1Model)
        {

            int retval = 0;
            try
            {
                retval = _MipTopic1services.Add(omiptopic1Model);
                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] miptopic1Model omiptopic1Model)
        {
            int retval = 0;
            try
            {
                retval = _MipTopic1services.Edit(omiptopic1Model);
                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] miptopic1Model omiptopic1Model)
        {
            int retval = 0;
            try
            {
                retval = _MipTopic1services.Delete(omiptopic1Model);
                return Ok(retval);
            }
            catch (Exception ex)
            {
                oCommonRepository.ErrorLog(ex);
                // return error message if there was an exception
                return BadRequest(new { message = ex.Message });
            }
        }
    }
}