using APT.BaseData.Domain.Entities.OP;
using APT.Infrastructure.Core;
using APT.Migrations;
using APT.Utility;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace APT.OP.WebApi.Controllers.Api
{
    [Route("api/OP/DbMigration")]
    public class DbMigrationController : AuthorizeTreeApiController<T_OP_MENU>
    {
        [HttpPost, Route("Migrate")]
        public JsonActionResult<bool> Migrate([FromBody] KeywordFilter filter)
        {
            return SafeExecute<bool>(() =>
            {
                var orgFilter = new BaseFilter();
                orgFilter.IgnoreOrgRule = true;
                var DbConnects = this.GetEntities<T_OP_TENANT_DB_CONN>(null, orgFilter);
                foreach (var DbConnect in DbConnects)
                {
                    using (var context = new MigrationContext(DbConnect.DB_CONN))
                    {
                        context.Database.Migrate();
                    }
                }
                return true;
            });

        }
    }
}
