2024. 7. 5. 19:55ใDevelopment๐ฉ๐ป๐ฆณ/C#
ํ๋ก์ ํธ ๋ง์ง๋ง ๊ตฌํ ์ฌํญ์ด๋ฉฐ API ์ฐ๋ ๋ฐ ๋ฐ์ดํฐ ๋ฒ ์ด์ค ์ฐ๋์
com์ ์ด์ฉํ ํต์ ์ฐํ ๋ฐฉ๋ฒ์ ์ฐ๊ธฐ ์ด์ ๋ฒ์ ์์ ๊ตฌํํ์ต๋๋ค
1. ํ๋ก์ ํธ ๊ฐ์ ๋ฐ ์ด๊ธฐ ์ค์
์๋ ํ์ธ์! ์ค๋์ ์ ๊ฐ ์ต๊ทผ ํ๋ก์ ํธ๋ฅผ ์งํํ๋ฉด์ ๊ฒช์๋ ๋ฐฑ์๋ ๊ด๋ จ ๊ตฌํ ์ฌํญ๊ณผ ์๋ฌ ํด๊ฒฐ ๊ณผ์ ์ ๊ณต์ ํ๋ ค๊ณ ํฉ๋๋ค. ์ด๋ฒ ํฌ์คํ ์ ์๋ฆฌ์ฆ ์ค ์ฒซ ๋ฒ์งธ๋ก, ํ๋ก์ ํธ ๊ฐ์์ ์ด๊ธฐ ์ค์ ์ ๋ํด ๋ค๋ฃฐ ์์ ์ ๋๋ค. ์ด ๊ณผ์ ์์ ๋ฐ์ํ ๋ฌธ์ ๋ค๊ณผ ๊ทธ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋จ๊ณ๋ณ๋ก ์ค๋ช ํ๊ฒ ์ต๋๋ค.
ํ๋ก์ ํธ ๊ฐ์: ์ด๋ฒ ํ๋ก์ ํธ๋ ์์ ์ง์์ ๊ด๋ฆฌ ์์คํ ์ ๊ตฌ์ถํ๋ ๊ฒ์ด ๋ชฉํ์์ต๋๋ค. ์ด ์์คํ ์ ๋ค์ํ ์์ ์ง์์๋ฅผ ์์ฑ, ์กฐํ, ์์ , ์ญ์ ํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. ๋ฐฑ์๋๋ ASP.NET Core์ Entity Framework Core๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ๋๋ก ์ค๊ณํ์ต๋๋ค. ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ MySQL์ ์ฌ์ฉํ์๊ณ , ์ฃผ์ ํ ์ด๋ธ์ **WorkInstruction**์ด๋ผ๋ ์์ ์ง์์๋ฅผ ์ ์ฅํ๋ ํ ์ด๋ธ์ด์์ต๋๋ค.
์ด๊ธฐ ์ค์
1. ํ๋ก์ ํธ ์์ฑ ๋ฐ ๊ธฐ๋ณธ ๊ตฌ์กฐ ์ค์ : ์ฒซ ๋จ๊ณ๋ก, ASP.NET Core ํ๋ก์ ํธ๋ฅผ ์์ฑํ์ต๋๋ค. ์ด ๊ณผ์ ์์ ๊ธฐ๋ณธ์ ์ธ ํ๋ก์ ํธ ๊ตฌ์กฐ๋ฅผ ์ค์ ํ๊ณ , ํ์ํ ํจํค์ง๋ฅผ ์ค์นํ์ต๋๋ค.
bash์ฝ๋ ๋ณต์ฌ
dotnet new webapi -n WorkManagementSystem
cd WorkManagementSystem
dotnet add package Microsoft.EntityFrameworkCore
dotnet add package Pomelo.EntityFrameworkCore.MySql
dotnet add package Microsoft.EntityFrameworkCore.Tools
dotnet add package Microsoft.Extensions.Logging
์ดํ, ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฐ๊ฒฐ์ ์ค์ ํ๊ธฐ ์ํด appsettings.json ํ์ผ์ MySQL ์ฐ๊ฒฐ ๋ฌธ์์ด์ ์ถ๊ฐํ์ต๋๋ค.
json์ฝ๋ ๋ณต์ฌ
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=plc_data_model1;User=root;Password=Alejd1785!;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปจํ ์คํธ ์ค์ : ๋ค์์ผ๋ก, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปจํ ์คํธ ํด๋์ค๋ฅผ ์์ฑํ์ต๋๋ค. AppDbContext ํด๋์ค๋ Entity Framework Core์์ DbContext๋ฅผ ์์๋ฐ์ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ํธ์์ฉ์ ๋ด๋นํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.EntityFrameworkCore;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
namespace WindowsFormsApp1Core.SQLModels
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<WorkInstruction> WorkInstructions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string connectionString = "Server=localhost;Database=plc_data_model1;User=root;Password=Alejd1785!;";
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
}
}
}
3. WorkInstruction ๋ชจ๋ธ ์ ์:WorkInstruction ๋ชจ๋ธ์ ์์ ์ง์์์ ๊ตฌ์กฐ๋ฅผ ์ ์ํฉ๋๋ค. ์ด ํด๋์ค๋ ์์ ์ง์์์ ๋ค์ํ ์์ฑ์ ํฌํจํ๊ณ ์์ต๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using System;
using System.ComponentModel.DataAnnotations;
namespace WindowsFormsApp1Core.WorkManagementSystem.WMModels
{
public class WorkInstruction
{
[Key]
public string TaskName { get; set; }
public string Content { get; set; }
public DateTime Date { get; set; }
public string Writer { get; set; }
public string Priority { get; set; }
public bool IsCompleted { get; set; }
}
}
4. DI ์ค์ ๋ฐ ์๋น์ค ๋ฑ๋ก: ์ด์ Program.cs ํ์ผ์์ **AppDbContext**์ **WorkInstructionService**๋ฅผ DI ์ปจํ ์ด๋์ ๋ฑ๋กํ์ฌ, ํ๋ก์ ํธ ์ ์ฒด์์ ์ฌ์ฉํ ์ ์๋๋ก ์ค์ ํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using WindowsFormsApp1Core.SQLModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMSrvs;
using Microsoft.Extensions.Logging;
public static class Program
{
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices((context, services) =>
{
services.AddDbContext<AppDbContext>(options =>
options.UseMySql(
context.Configuration.GetConnectionString("DefaultConnection"),
ServerVersion.AutoDetect(context.Configuration.GetConnectionString("DefaultConnection"))
));
services.AddScoped<WorkInstructionService>();
services.AddControllersWithViews();
});
webBuilder.Configure((context, app) =>
{
if (context.HostingEnvironment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
});
});
});
}
5. ์๋น์ค ํด๋์ค ๊ตฌํ:WorkInstructionService ํด๋์ค๋ ์ค์ ๋น์ฆ๋์ค ๋ก์ง์ ๊ตฌํํฉ๋๋ค. ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์์ ์ง์์๋ฅผ ์์ฑ, ์กฐํ, ์์ , ์ญ์ ํ๋ ๋ฉ์๋๋ฅผ ํฌํจํ๊ณ ์์ต๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using System.Collections.Generic;
using System.Threading.Tasks;
using WindowsFormsApp1Core.SQLModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.WorkManagementSystem.WMSrvs
{
public class WorkInstructionService
{
private readonly AppDbContext _context;
private readonly ILogger<WorkInstructionService> _logger;
public WorkInstructionService(AppDbContext context, ILogger<WorkInstructionService> logger)
{
_context = context;
_logger = logger;
}
public async Task<IEnumerable<WorkInstruction>> GetWorkInstructionsAsync()
{
try
{
return await _context.WorkInstructions.ToListAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
throw;
}
}
public async Task AddWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
_context.WorkInstructions.Add(workInstruction);
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
throw;
}
}
public async Task UpdateWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
var existing = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == workInstruction.TaskName);
if (existing != null)
{
existing.Content = workInstruction.Content;
existing.Date = workInstruction.Date;
existing.Writer = workInstruction.Writer;
existing.Priority = workInstruction.Priority;
existing.IsCompleted = workInstruction.IsCompleted;
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", workInstruction.TaskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
throw;
}
}
public async Task DeleteWorkInstructionAsync(string taskName)
{
try
{
var workInstruction = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
if (workInstruction != null)
{
_context.WorkInstructions.Remove(workInstruction);
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", taskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
throw;
}
}
public async Task<WorkInstruction> GetWorkInstructionByTaskNameAsync(string taskName)
{
try
{
return await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instruction by task name");
throw;
}
}
}
}
6. ์ปจํธ๋กค๋ฌ ๊ตฌํ: ๋ง์ง๋ง์ผ๋ก, WorkInstructionController ํด๋์ค๋ฅผ ์์ฑํ์ฌ ํด๋ผ์ด์ธํธ ์์ฒญ์ ์ฒ๋ฆฌํฉ๋๋ค. ์ด ์ปจํธ๋กค๋ฌ๋ RESTful API๋ฅผ ์ ๊ณตํ์ฌ ์์ ์ง์์๋ฅผ ๊ด๋ฆฌํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMSrvs;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.Dashboard.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class WorkInstructionController : ControllerBase
{
private readonly WorkInstructionService _service;
private readonly ILogger<WorkInstructionController> _logger;
public WorkInstructionController(AppDbContext context, ILogger<WorkInstructionController> logger)
{
_service = new WorkInstructionService(context, logger);
_logger = logger;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<WorkInstruction>>> Get()
{
try
{
var workInstructions = await _service.GetWorkInstructionsAsync();
return Ok(workInstructions);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
return StatusCode(500, "Internal server error");
}
}
[HttpPost]
public async Task<ActionResult<WorkInstruction>> Post(WorkInstruction workInstruction)
{
try
{
await _service.AddWorkInstructionAsync(workInstruction);
return CreatedAtAction(nameof(Get), new { id = workInstruction.TaskName }, workInstruction);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpPut("{taskName}")]
public async Task<IActionResult> Put(string taskName, WorkInstruction workInstruction)
{
try
{
var existingWorkInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (existingWorkInstruction == null)
{
return NotFound();
}
await _service.UpdateWorkInstructionAsync(workInstruction);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpDelete("{taskName}")]
public async Task<IActionResult> Delete(string taskName)
{
try
{
var workInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (workInstruction == null)
{
return NotFound();
}
await _service.DeleteWorkInstructionAsync(taskName);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
return StatusCode(500, "Internal server error");
}
}
}
}
๊ฒฐ๋ก :
์ด์ ๊ฐ์ด ํ๋ก์ ํธ์ ์ด๊ธฐ ์ค์ ์ ์๋ฃํ์ต๋๋ค. ๋ค์ ํฌ์คํ ์์๋ ๋ณธ๊ฒฉ์ ์ผ๋ก ๋ฐ์ํ ์ค๋ฅ์ ๊ทธ ํด๊ฒฐ ๋ฐฉ๋ฒ์ ๋ค๋ฃจ๊ฒ ์ต๋๋ค. ์ด ๊ณผ์ ์์ Entity Framework Core์ ๊ด๋ จ๋ ๋ค์ํ ๋ฌธ์ ์ ์ด๋ฅผ ํด๊ฒฐํ๋ ๊ณผ์ ์ ํตํด ์ฌ๋ฌ๋ถ๋ ๋น์ทํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ ์ ์๋๋ก ๋๊ฒ ์ต๋๋ค.
2. ์๋ฌ ๋ถ์ ๋ฐ ํด๊ฒฐ (API)
ํ๋ก์ ํธ ์งํ ์ค์ ๋ง์ฃผํ API ๊ด๋ จ ์ค๋ฅ๋ฅผ ๋ถ์ํ๊ณ ํด๊ฒฐํ๋ ๊ณผ์ ์ ๊ณต์ ํ๊ฒ ์ต๋๋ค. ํ๋ก์ ํธ ์ด๊ธฐ ์ค์ ์ดํ, ์ค์ ๊ธฐ๋ฅ์ ๊ตฌํํ๋ฉด์ ๋ค์ํ ์๋ฌ๊ฐ ๋ฐ์ํ๋๋ฐ, ์ด ๊ณผ์ ์์ ๋ฐฐ์ฐ๊ณ ํด๊ฒฐํ ๋ด์ฉ์ ๋จ๊ณ๋ณ๋ก ์ ๋ฆฌํ์์ต๋๋ค.
์๋ฌ ๋ถ์
1. CS1061 ์ค๋ฅ:
์ค๋ฅ ๋ฉ์์ง: 'WorkInstructionService' does not contain a definition for 'DeleteWorkInstruction' and no accessible extension method 'DeleteWorkInstruction' accepting a first argument of type 'WorkInstructionService' could be found.
์์ธ ๋ถ์:
- WorkInstructionService ํด๋์ค์ DeleteWorkInstruction, GetWorkInstructions, AddWorkInstruction ๋ฑ์ ๋ฉ์๋ ์ ์๊ฐ ์๋ค๋ ๋ฉ์์ง์ ๋๋ค.
- AppDbContext ํด๋์ค์ WorkInstructions ์์ฑ์ด ์๋ค๋ ๊ฒ์ ์๋ฏธํ ์ ์์ต๋๋ค.
2. CS7036 ์ค๋ฅ:
์ค๋ฅ ๋ฉ์์ง: There is no argument given that corresponds to the required formal parameter 'context' of 'WorkInstructionService.WorkInstructionService(AppDbContext)'.
์์ธ ๋ถ์:
- **WorkInstructionService**์ ์์ฑ์๊ฐ **context**๋ผ๋ AppDbContext ํ์ ์ ๋งค๊ฐ๋ณ์๋ฅผ ํ์๋ก ํ์ง๋ง, ์ด๋ฅผ ์ ๊ณตํ์ง ์์์ต๋๋ค.
์ค๋ฅ ํด๊ฒฐ ๋จ๊ณ
๋จ๊ณ 1: ํด๋์ค ์ ์ ๋ฐ ๋ฉ์๋ ํ์ธ
- WorkInstructionService ํด๋์ค์ ๋ชจ๋ ํ์ํ ๋ฉ์๋(DeleteWorkInstruction, GetWorkInstructions, AddWorkInstruction ๋ฑ)๊ฐ ์ ์๋์ด ์๋์ง ํ์ธํฉ๋๋ค.
- AppDbContext ํด๋์ค์ WorkInstructions DbSet์ด ์ ์๋์ด ์๋์ง ํ์ธํฉ๋๋ค.
๋จ๊ณ 2: ์์ฑ์ ๋งค๊ฐ๋ณ์ ํ์ธ
- **WorkInstructionService**๋ฅผ ์ธ์คํด์คํํ ๋ AppDbContext ์ธ์คํด์ค๋ฅผ ์ ๋ฌํ๋์ง ํ์ธํฉ๋๋ค.
๋จ๊ณ 3: Using ๋ฌธ ๋ฐ ์ฐธ์กฐ ํ์ธ
- ํ์ผ ์๋จ์ ํ์ํ ๋ค์์คํ์ด์ค์ ๋ํ using ๋ฌธ์ด ์๋์ง ํ์ธํฉ๋๋ค.
ํ์ผ ๊ฒํ
1. WorkInstructionService.cs
์ฐ์ WorkInstructionService ํด๋์ค์ ๋ฉ์๋๋ค์ด ์ ์๋์ด ์๋์ง ํ์ธํ๊ฒ ์ต๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using System.Collections.Generic;
using System.Threading.Tasks;
using WindowsFormsApp1Core.SQLModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.WorkManagementSystem.WMSrvs
{
public class WorkInstructionService
{
private readonly AppDbContext _context;
private readonly ILogger<WorkInstructionService> _logger;
public WorkInstructionService(AppDbContext context, ILogger<WorkInstructionService> logger)
{
_context = context;
_logger = logger;
}
public async Task<IEnumerable<WorkInstruction>> GetWorkInstructionsAsync()
{
try
{
return await _context.WorkInstructions.ToListAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
throw;
}
}
public async Task AddWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
_context.WorkInstructions.Add(workInstruction);
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
throw;
}
}
public async Task UpdateWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
var existing = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == workInstruction.TaskName);
if (existing != null)
{
existing.Content = workInstruction.Content;
existing.Date = workInstruction.Date;
existing.Writer = workInstruction.Writer;
existing.Priority = workInstruction.Priority;
existing.IsCompleted = workInstruction.IsCompleted;
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", workInstruction.TaskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
throw;
}
}
public async Task DeleteWorkInstructionAsync(string taskName)
{
try
{
var workInstruction = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
if (workInstruction != null)
{
_context.WorkInstructions.Remove(workInstruction);
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", taskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
throw;
}
}
public async Task<WorkInstruction> GetWorkInstructionByTaskNameAsync(string taskName)
{
try
{
return await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instruction by task name");
throw;
}
}
}
}
2. AppDbContext.cs
์ด์ AppDbContext ํด๋์ค์ WorkInstructions DbSet์ด ์ ์๋์ด ์๋์ง ํ์ธํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.EntityFrameworkCore;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
namespace WindowsFormsApp1Core.SQLModels
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<WorkInstruction> WorkInstructions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string connectionString = "Server=localhost;Database=plc_data_model1;User=root;Password=Alejd1785!;";
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
}
}
}
3. WorkInstructionServiceCtrl.cs
WorkInstructionService ์ธ์คํด์ค๋ฅผ ์์ฑํ ๋ **AppDbContext**๋ฅผ ์ ๋ฌํ๋์ง ํ์ธํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMSrvs;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.Dashboard.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class WorkInstructionController : ControllerBase
{
private readonly WorkInstructionService _service;
private readonly ILogger<WorkInstructionController> _logger;
public WorkInstructionController(AppDbContext context, ILogger<WorkInstructionController> logger)
{
_service = new WorkInstructionService(context, logger);
_logger = logger;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<WorkInstruction>>> Get()
{
try
{
var workInstructions = await _service.GetWorkInstructionsAsync();
return Ok(workInstructions);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
return StatusCode(500, "Internal server error");
}
}
[HttpPost]
public async Task<ActionResult<WorkInstruction>> Post(WorkInstruction workInstruction)
{
try
{
await _service.AddWorkInstructionAsync(workInstruction);
return CreatedAtAction(nameof(Get), new { id = workInstruction.TaskName }, workInstruction);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpPut("{taskName}")]
public async Task<IActionResult> Put(string taskName, WorkInstruction workInstruction)
{
try
{
var existingWorkInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (existingWorkInstruction == null)
{
return NotFound();
}
await _service.UpdateWorkInstructionAsync(workInstruction);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpDelete("{taskName}")]
public async Task<IActionResult> Delete(string taskName)
{
try
{
var workInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (workInstruction == null)
{
return NotFound();
}
await _service.DeleteWorkInstructionAsync(taskName);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
return StatusCode(500, "Internal server error");
}
}
}
}
500 ๋ด๋ถ ์๋ฒ ์ค๋ฅ ํด๊ฒฐ
๋ฌธ์ : 500 ๋ด๋ถ ์๋ฒ ์ค๋ฅ๋ ์ฃผ๋ก ์๋ฒ์์ ๋ฐ์ํ๋ ์์ธ๋ก ์ธํด ์๋ต์ด ์คํจํ๋ ๊ฒฝ์ฐ์ ๋๋ค. ์ด ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๊ธฐ ์ํด ๋ช ๊ฐ์ง ์ ๊ฒ ์ฌํญ์ ํ์ธํด ๋ณด์์ต๋๋ค.
ํ์ธํ ํญ๋ชฉ๋ค:
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ๋ฌธ์์ด:
- appsettings.json ๋๋ **AppDbContext**์์ ์ฌ์ฉํ ์ฐ๊ฒฐ ๋ฌธ์์ด์ด ์ฌ๋ฐ๋ฅธ์ง ํ์ธํฉ๋๋ค.
- WorkInstructionService ํด๋์ค์ ๊ตฌํ:
- ์ด ์๋น์ค ํด๋์ค์ ๋ฉ์๋๋ค์ด ์ฌ๋ฐ๋ฅด๊ฒ ์ ์๋์ด ์๋์ง ํ์ธํฉ๋๋ค.
- ์์กด์ฑ ์ฃผ์
(DI) ์ค์ :
- **Program.cs**์์ **AppDbContext**์ **WorkInstructionService**๊ฐ ์ ๋๋ก ๋ฑ๋ก๋์๋์ง ํ์ธํฉ๋๋ค.
๊ฒฐ๋ก
์ด๋ฒ ํฌ์คํ ์์๋ API ๊ด๋ จ ์ค๋ฅ๋ฅผ ๋ถ์ํ๊ณ ํด๊ฒฐํ๋ ๊ณผ์ ์ ์ดํด๋ณด์์ต๋๋ค. ์๋ฌ ๋ฉ์์ง๋ฅผ ๊ธฐ๋ฐ์ผ๋ก ๋ฌธ์ ๋ฅผ ๋ถ์ํ๊ณ , ํ์ํ ์์ ์ฌํญ์ ์ ์ฉํ์ฌ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๋ฐฉ๋ฒ์ ๋ฐฐ์ ์ต๋๋ค. ๋ค์ ํฌ์คํ ์์๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ง์ด๊ทธ๋ ์ด์ ๋ฐ ๊ธฐํ ๋ฐ์ํ ๋ฌธ์ ๋ฅผ ํด๊ฒฐํ๋ ๊ณผ์ ์ ๋ค๋ฃจ๊ฒ ์ต๋๋ค.
4. ์์กด์ฑ ์ค์น ๋ฐ ์ค์
์ด ํฌ์คํ ์์๋ ํ๋ก์ ํธ์ ํ์ํ ์์กด์ฑ ์ค์น ๋ฐ ์ค์ ๋ฐฉ๋ฒ์ ๋ค๋ฃจ๊ฒ ์ต๋๋ค. ํนํ, NuGet ํจํค์ง๋ฅผ ์ค์นํ๊ณ , ํ๋ก์ ํธ์ ์ค์ ํ์ผ๋ค์ ์์ ํ๋ ๊ณผ์ ์ ์ค๋ช ํฉ๋๋ค. ์ด๋ฌํ ์ค์ ์ ํ๋ก์ ํธ๊ฐ ์ ์์ ์ผ๋ก ๋น๋๋๊ณ ์คํ๋๊ธฐ ์ํด ํ์์ ์ ๋๋ค.
NuGet ํจํค์ง ์ค์น
ํ๋ก์ ํธ์์ ์ฌ์ฉํ NuGet ํจํค์ง๋ฅผ ์ค์นํฉ๋๋ค. ์ฌ๊ธฐ์์๋ Pomelo.EntityFrameworkCore.MySql, Microsoft.EntityFrameworkCore, Microsoft.EntityFrameworkCore.Tools ํจํค์ง๋ฅผ ์ค์นํฉ๋๋ค.
1. ํจํค์ง ์ค์น ๋ช ๋ น
ํจํค์ง ๊ด๋ฆฌ ์ฝ์์์ ๋ค์ ๋ช ๋ น์ ์คํํ์ฌ ํ์ํ ํจํค์ง๋ฅผ ์ค์นํฉ๋๋ค.
powershell์ฝ๋ ๋ณต์ฌ
Install-Package Pomelo.EntityFrameworkCore.MySql
Install-Package Microsoft.EntityFrameworkCore
Install-Package Microsoft.EntityFrameworkCore.Tools
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ์ค์
1. appsettings.json ์ค์
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ๋ฌธ์์ด์ ์ค์ ํ์ฌ ํ๋ก์ ํธ๊ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ๊ทผํ ์ ์๋๋ก ํฉ๋๋ค.
json์ฝ๋ ๋ณต์ฌ
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=plc_data_model1;User=root;Password=Alejd1785!;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
AppDbContext ํด๋์ค ์ค์
1. AppDbContext.cs ์์
AppDbContext ํด๋์ค์ WorkInstructions DbSet์ ์ถ๊ฐํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ชจ๋ธ์ ์ค์ ํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.EntityFrameworkCore;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
namespace WindowsFormsApp1Core.SQLModels
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<WorkInstruction> WorkInstructions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string connectionString = "Server=localhost;Database=plc_data_model1;User=root;Password=Alejd1785!;";
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
}
}
}
์์กด์ฑ ์ฃผ์ ์ค์
1. Program.cs ์์
ํ๋ก์ ํธ์ DI ์ปจํ ์ด๋์ **AppDbContext**์ **WorkInstructionService**๋ฅผ ๋ฑ๋กํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore;
using WindowsFormsApp1Core.SQLModels;
using Microsoft.Extensions.Logging;
namespace WorkManagementSystem
{
public class Program
{
public static void Main(string[] args)
{
CreateHostBuilder(args).Build().Run();
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices((context, services) =>
{
services.AddDbContext<AppDbContext>(options =>
options.UseMySql(
context.Configuration.GetConnectionString("DefaultConnection"),
ServerVersion.AutoDetect(context.Configuration.GetConnectionString("DefaultConnection"))
));
services.AddScoped<WorkInstructionService>();
services.AddLogging();
services.AddControllersWithViews();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSyncfusionBlazor();
});
webBuilder.Configure((context, app) =>
{
var env = context.HostingEnvironment;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapRazorPages();
endpoints.MapFallbackToPage("/_Host");
});
});
});
}
}
๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ง์ด๊ทธ๋ ์ด์ ๋ฐ ์ ๋ฐ์ดํธ
1. ๋ง์ด๊ทธ๋ ์ด์ ์คํ
Entity Framework Core๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ง์ด๊ทธ๋ ์ด์ ์ ์คํํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง๋ฅผ ์ ๋ฐ์ดํธํฉ๋๋ค.
powershell์ฝ๋ ๋ณต์ฌ
Add-Migration InitialCreate
Update-Database
2. ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ด๊ธฐํ
Update-Database ๋ช ๋ น์ ์คํํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค๋ฅผ ์ต์ ์ํ๋ก ์ ๋ฐ์ดํธํฉ๋๋ค. ์ด๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฒ ์ด์ค ์คํค๋ง๊ฐ ํ๋ก์ ํธ์ ๋ชจ๋ธ๊ณผ ์ผ์นํ๊ฒ ๋ฉ๋๋ค.
๊ฒฐ๋ก
์ด๋ฒ ํฌ์คํ ์์๋ ํ๋ก์ ํธ์ ๋ฐฑ์๋ ์ค์ ์ ์ํ ์์กด์ฑ ์ค์น์ ์ค์ ๋ฐฉ๋ฒ์ ๋ค๋ฃจ์์ต๋๋ค. NuGet ํจํค์ง๋ฅผ ์ค์นํ๊ณ , ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ์ ์ค์ ํ๋ฉฐ, DI ์ปจํ ์ด๋์ ํ์ํ ์๋น์ค๋ฅผ ๋ฑ๋กํ๋ ๊ณผ์ ์ ์ดํด๋ณด์์ต๋๋ค. ์ด๋ฌํ ์ค์ ์ด ์๋ฃ๋๋ฉด ํ๋ก์ ํธ๊ฐ ์ ์์ ์ผ๋ก ์คํ๋๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ์์ฉํ ์ ์๊ฒ ๋ฉ๋๋ค. ๋ค์ ํฌ์คํ ์์๋ ํ๋ก์ ํธ์ ํ๋ก ํธ์๋์ ๊ด๋ จ๋ ์ค์ ๋ฐ ๊ตฌํ ์ฌํญ์ ๋ค๋ฃฐ ์์ ์ ๋๋ค.
5. WorkInstructionService ํด๋์ค์ ์ปจํธ๋กค๋ฌ ๊ตฌํ
์๋ก : ์ด ํฌ์คํ ์์๋ WorkInstructionService ํด๋์ค์ ์ด๋ฅผ ์ฌ์ฉํ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃน๋๋ค. WorkInstructionService ํด๋์ค๋ ์์ ์ง์์์ CRUD ์์ ์ ๋ด๋นํ๋ฉฐ, ์ปจํธ๋กค๋ฌ๋ ์ด๋ฌํ ์๋น์ค๋ฅผ ํธ์ถํ์ฌ ํด๋ผ์ด์ธํธ ์์ฒญ์ ์ฒ๋ฆฌํฉ๋๋ค.
WorkInstructionService ํด๋์ค ๊ตฌํ
WorkInstructionService ํด๋์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์์ ์ง์์๋ฅผ ๊ด๋ฆฌํ๋ ๋น์ฆ๋์ค ๋ก์ง์ ํฌํจํฉ๋๋ค.
1. WorkInstructionService.cs
csharp์ฝ๋ ๋ณต์ฌ
using System.Collections.Generic;
using System.Threading.Tasks;
using WindowsFormsApp1Core.SQLModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.WorkManagementSystem.WMSrvs
{
public class WorkInstructionService
{
private readonly AppDbContext _context;
private readonly ILogger<WorkInstructionService> _logger;
public WorkInstructionService(AppDbContext context, ILogger<WorkInstructionService> logger)
{
_context = context;
_logger = logger;
}
public async Task<IEnumerable<WorkInstruction>> GetWorkInstructionsAsync()
{
try
{
return await _context.WorkInstructions.ToListAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
throw;
}
}
public async Task AddWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
_context.WorkInstructions.Add(workInstruction);
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
throw;
}
}
public async Task UpdateWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
var existing = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == workInstruction.TaskName);
if (existing != null)
{
existing.Content = workInstruction.Content;
existing.Date = workInstruction.Date;
existing.Writer = workInstruction.Writer;
existing.Priority = workInstruction.Priority;
existing.IsCompleted = workInstruction.IsCompleted;
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", workInstruction.TaskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
throw;
}
}
public async Task DeleteWorkInstructionAsync(string taskName)
{
try
{
var workInstruction = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
if (workInstruction != null)
{
_context.WorkInstructions.Remove(workInstruction);
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", taskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
throw;
}
}
public async Task<WorkInstruction> GetWorkInstructionByTaskNameAsync(string taskName)
{
try
{
return await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instruction by task name");
throw;
}
}
}
}
WorkInstructionController ํด๋์ค ๊ตฌํ
์ปจํธ๋กค๋ฌ๋ **WorkInstructionService**๋ฅผ ์ฌ์ฉํ์ฌ ํด๋ผ์ด์ธํธ ์์ฒญ์ ์ฒ๋ฆฌํฉ๋๋ค.
2. WorkInstructionController.cs
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMSrvs;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.Dashboard.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class WorkInstructionController : ControllerBase
{
private readonly WorkInstructionService _service;
private readonly ILogger<WorkInstructionController> _logger;
public WorkInstructionController(WorkInstructionService service, ILogger<WorkInstructionController> logger)
{
_service = service;
_logger = logger;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<WorkInstruction>>> Get()
{
try
{
var workInstructions = await _service.GetWorkInstructionsAsync();
return Ok(workInstructions);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
return StatusCode(500, "Internal server error");
}
}
[HttpPost]
public async Task<ActionResult<WorkInstruction>> Post(WorkInstruction workInstruction)
{
try
{
await _service.AddWorkInstructionAsync(workInstruction);
return CreatedAtAction(nameof(Get), new { id = workInstruction.TaskName }, workInstruction);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpPut("{taskName}")]
public async Task<IActionResult> Put(string taskName, WorkInstruction workInstruction)
{
try
{
var existingWorkInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (existingWorkInstruction == null)
{
return NotFound();
}
await _service.UpdateWorkInstructionAsync(workInstruction);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpDelete("{taskName}")]
public async Task<IActionResult> Delete(string taskName)
{
try
{
var workInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (workInstruction == null)
{
return NotFound();
}
await _service.DeleteWorkInstructionAsync(taskName);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
return StatusCode(500, "Internal server error");
}
}
}
}
๊ฒฐ๋ก
์ด๋ฒ ํฌ์คํ ์์๋ WorkInstructionService ํด๋์ค์ ์ด๋ฅผ ์ฌ์ฉํ๋ ์ปจํธ๋กค๋ฌ๋ฅผ ๊ตฌํํ๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃจ์์ต๋๋ค. WorkInstructionService ํด๋์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์์ ์ง์์๋ฅผ ๊ด๋ฆฌํ๋ ๋น์ฆ๋์ค ๋ก์ง์ ํฌํจํ๊ณ ์์ผ๋ฉฐ, ์ปจํธ๋กค๋ฌ๋ ์ด๋ฌํ ์๋น์ค๋ฅผ ํธ์ถํ์ฌ ํด๋ผ์ด์ธํธ ์์ฒญ์ ์ฒ๋ฆฌํฉ๋๋ค. ๋ค์ ํฌ์คํ ์์๋ ํ๋ก์ ํธ์ ํ๋ก ํธ์๋์ ๊ด๋ จ๋ ์ค์ ๋ฐ ๊ตฌํ ์ฌํญ์ ๋ค๋ฃฐ ์์ ์ ๋๋ค.
6. ์์กด์ฑ ์ฃผ์ ๋ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์
์๋ก : ์ด ํฌ์คํ ์์๋ ์์กด์ฑ ์ฃผ์ (DI)๊ณผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์ ์ ๋ค๋ฃน๋๋ค. DI๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๊ตฌ์ฑ ์์๋ฅผ ์๋ก ๋ถ๋ฆฌํ์ฌ ์ ์ง ๊ด๋ฆฌ์ ํ ์คํธ๋ฅผ ์ฉ์ดํ๊ฒ ํ๊ณ , ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์ ์ ์ ํ๋ฆฌ์ผ์ด์ ์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ ์ ์๋๋ก ๊ตฌ์ฑํ๋ ์ค์ํ ๋จ๊ณ์ ๋๋ค.
์์กด์ฑ ์ฃผ์ ์ค์
์์กด์ฑ ์ฃผ์ ์ ์ ํ๋ฆฌ์ผ์ด์ ์ ํด๋์ค๋ค์ด ํ์ํ ์ข ์์ฑ์ ์ธ๋ถ์์ ์ฃผ์ ๋ฐ๋๋ก ํ๋ ํจํด์ ๋๋ค. ์ด๋ฅผ ํตํด ํด๋์ค ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ ์ฐ์ฑ์ ๋์ผ ์ ์์ต๋๋ค.
1. Program.cs
Program.cs ํ์ผ์์ ์๋น์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ปจํ ์คํธ๋ฅผ ์ค์ ํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using WindowsFormsApp1Core.SQLModels;
using Syncfusion.Blazor;
using System;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using WindowsFormsApp1Core.WorkManagementSystem.WMSrvs;
namespace WorkManagementSystem
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.SetCompatibleTextRenderingDefault(false);
var host = CreateHostBuilder(args).Build();
host.Start();
Application.Run(new FormMain());
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices((context, services) =>
{
services.AddDbContext<AppDbContext>(options =>
options.UseMySql(
context.Configuration.GetConnectionString("DefaultConnection"),
ServerVersion.AutoDetect(context.Configuration.GetConnectionString("DefaultConnection"))
));
// Add WorkInstructionService to the DI container
services.AddScoped<WorkInstructionService>();
services.AddControllersWithViews();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSyncfusionBlazor();
});
webBuilder.Configure((context, app) =>
{
var env = context.HostingEnvironment;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapRazorPages();
endpoints.MapFallbackToPage("/_Host");
});
});
});
}
}
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์ ์ ์ ํ๋ฆฌ์ผ์ด์ ์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ ์ ์๋๋ก ๊ตฌ์ฑํ๋ ์ค์ํ ๋จ๊ณ์ ๋๋ค. ์ด๋ฅผ ์ํด appsettings.json ํ์ผ๊ณผ AppDbContext ํด๋์ค๋ฅผ ์ค์ ํฉ๋๋ค.
2. appsettings.json
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ๋ฌธ์์ด์ ์ค์ ํฉ๋๋ค.
json์ฝ๋ ๋ณต์ฌ
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=plc_data_model1;User=root;Password=Alejd1785!;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
3. AppDbContext.cs
AppDbContext ํด๋์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฐ๊ฒฐ์ ๊ด๋ฆฌํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.EntityFrameworkCore;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
namespace WindowsFormsApp1Core.SQLModels
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<PlcDataModel> PlcDatas { get; set; }
public DbSet<Table1> Table1s { get; set; }
public DbSet<Table2> Table2s { get; set; }
public DbSet<WorkInstruction> WorkInstructions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string connectionString = "Server=localhost;Database=plc_data_model1;User=root;Password=Alejd1785!;";
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
}
}
}
๊ฒฐ๋ก
์ด๋ฒ ํฌ์คํ ์์๋ ์์กด์ฑ ์ฃผ์ ๊ณผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์ ๋ฐฉ๋ฒ์ ๋ค๋ฃจ์์ต๋๋ค. ์์กด์ฑ ์ฃผ์ ์ ํตํด ํด๋์ค ๊ฐ์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ์ ์ฐ์ฑ์ ๋์์ผ๋ฉฐ, ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ค์ ์ ํตํด ์ ํ๋ฆฌ์ผ์ด์ ์ด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํํ๊ฒ ์ํธ ์์ฉํ ์ ์๋๋ก ๊ตฌ์ฑํ์ต๋๋ค. ๋ค์ ํฌ์คํ ์์๋ ํ๋ก์ ํธ์ ์ ๋ ํ ์คํธ์ ๋๋ฒ๊น ๋ฐฉ๋ฒ์ ๋ํด ๋ค๋ฃฐ ์์ ์ ๋๋ค.
7. ์๋น์ค์ ์ปจํธ๋กค๋ฌ ๊ตฌํ
์๋ก : ์ด๋ฒ ํฌ์คํ ์์๋ **WorkInstructionService**์ **WorkInstructionController**๋ฅผ ๊ตฌํํ๋ ๊ณผ์ ์ ๋ค๋ฃน๋๋ค. ์ด ๊ณผ์ ์ ๋น์ฆ๋์ค ๋ก์ง๊ณผ API ์๋ํฌ์ธํธ๋ฅผ ๋ถ๋ฆฌํ์ฌ ์ฝ๋์ ๊ฐ๋ ์ฑ๊ณผ ์ ์ง ๋ณด์์ฑ์ ๋์ด๋ ์ค์ํ ๋จ๊ณ์ ๋๋ค.
์๋น์ค ํด๋์ค ๊ตฌํ
์๋น์ค ํด๋์ค๋ ๋น์ฆ๋์ค ๋ก์ง์ ์ฒ๋ฆฌํ๋ ๋ฐ ์ฌ์ฉ๋ฉ๋๋ค. WorkInstructionService ํด๋์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ๋ฉฐ, CRUD ์์ ์ ์ํํฉ๋๋ค.
1. WorkInstructionService ํด๋์ค
๋ค์์ WorkInstructionService ํด๋์ค์ ๊ตฌํ ์์ ์ ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using System.Collections.Generic;
using System.Threading.Tasks;
using WindowsFormsApp1Core.SQLModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.WorkManagementSystem.WMSrvs
{
public class WorkInstructionService
{
private readonly AppDbContext _context;
private readonly ILogger<WorkInstructionService> _logger;
public WorkInstructionService(AppDbContext context, ILogger<WorkInstructionService> logger)
{
_context = context;
_logger = logger;
}
public async Task<IEnumerable<WorkInstruction>> GetWorkInstructionsAsync()
{
try
{
return await _context.WorkInstructions.ToListAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
throw;
}
}
public async Task AddWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
_context.WorkInstructions.Add(workInstruction);
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
throw;
}
}
public async Task UpdateWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
var existing = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == workInstruction.TaskName);
if (existing != null)
{
existing.Content = workInstruction.Content;
existing.Date = workInstruction.Date;
existing.Writer = workInstruction.Writer;
existing.Priority = workInstruction.Priority;
existing.IsCompleted = workInstruction.IsCompleted;
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", workInstruction.TaskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
throw;
}
}
public async Task DeleteWorkInstructionAsync(string taskName)
{
try
{
var workInstruction = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
if (workInstruction != null)
{
_context.WorkInstructions.Remove(workInstruction);
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", taskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
throw;
}
}
public async Task<WorkInstruction> GetWorkInstructionByTaskNameAsync(string taskName)
{
try
{
return await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instruction by task name");
throw;
}
}
}
}
์ปจํธ๋กค๋ฌ ํด๋์ค ๊ตฌํ
์ปจํธ๋กค๋ฌ ํด๋์ค๋ HTTP ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , ์๋น์ค ํด๋์ค์ ์ํธ ์์ฉํ์ฌ ํด๋ผ์ด์ธํธ์๊ฒ ์๋ต์ ๋ฐํํฉ๋๋ค.
2. WorkInstructionController ํด๋์ค
๋ค์์ WorkInstructionController ํด๋์ค์ ๊ตฌํ ์์ ์ ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMSrvs;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.Dashboard.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class WorkInstructionController : ControllerBase
{
private readonly WorkInstructionService _service;
private readonly ILogger<WorkInstructionController> _logger;
public WorkInstructionController(WorkInstructionService service, ILogger<WorkInstructionController> logger)
{
_service = service;
_logger = logger;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<WorkInstruction>>> Get()
{
try
{
var workInstructions = await _service.GetWorkInstructionsAsync();
return Ok(workInstructions);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
return StatusCode(500, "Internal server error");
}
}
[HttpPost]
public async Task<ActionResult<WorkInstruction>> Post(WorkInstruction workInstruction)
{
try
{
await _service.AddWorkInstructionAsync(workInstruction);
return CreatedAtAction(nameof(Get), new { id = workInstruction.TaskName }, workInstruction);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpPut("{taskName}")]
public async Task<IActionResult> Put(string taskName, WorkInstruction workInstruction)
{
try
{
var existingWorkInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (existingWorkInstruction == null)
{
return NotFound();
}
await _service.UpdateWorkInstructionAsync(workInstruction);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpDelete("{taskName}")]
public async Task<IActionResult> Delete(string taskName)
{
try
{
var workInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (workInstruction == null)
{
return NotFound();
}
await _service.DeleteWorkInstructionAsync(taskName);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
return StatusCode(500, "Internal server error");
}
}
}
}
๊ฒฐ๋ก
์ด๋ฒ ํฌ์คํ ์์๋ **WorkInstructionService**์ **WorkInstructionController**์ ๊ตฌํ ๊ณผ์ ์ ๋ค๋ฃจ์์ต๋๋ค. ์๋น์ค ํด๋์ค๋ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ๋ฉฐ, ์ปจํธ๋กค๋ฌ ํด๋์ค๋ HTTP ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ์๋น์ค๋ฅผ ํธ์ถํ์ฌ ํด๋ผ์ด์ธํธ์๊ฒ ์๋ต์ ๋ฐํํฉ๋๋ค. ๋ค์ ํฌ์คํ ์์๋ ํ๋ก์ ํธ์ ์ ๋ ํ ์คํธ์ ๋๋ฒ๊น ๋ฐฉ๋ฒ์ ๋ํด ๋ค๋ฃฐ ์์ ์ ๋๋ค.
8. ์์กด์ฑ ์ฃผ์ ์ค์ ๋ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ
์๋ก : ์ด๋ฒ ํฌ์คํ ์์๋ **WorkInstructionService**์ **AppDbContext**๋ฅผ ํ๋ก์ ํธ์ ์์กด์ฑ ์ฃผ์ (DI)ํ์ฌ ์ค์ ํ๊ณ ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฐ๊ฒฐ์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃน๋๋ค. ์ฌ๋ฐ๋ฅธ DI ์ค์ ์ ์ฝ๋์ ๋ชจ๋ํ์ ํ ์คํธ ๊ฐ๋ฅ์ฑ์ ๋์ด๋ ์ค์ํ ๋จ๊ณ์ ๋๋ค.
์์กด์ฑ ์ฃผ์ ์ค์
์์กด์ฑ ์ฃผ์ ์ ๊ฐ์ฒด ๊ฐ์ ์์กด์ฑ์ ์ค์ ํ๋ ๋์์ธ ํจํด์ผ๋ก, ๊ฐ์ฒด ์์ฑ๊ณผ ๋ผ์ดํ์ฌ์ดํด ๊ด๋ฆฌ๋ฅผ ํ๋ ์์ํฌ์ ๋งก๊ธฐ๋ ๋ฐฉ์์ ๋๋ค. ์ด๋ฅผ ํตํด ์ฝ๋์ ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ๊ณ ํ ์คํธ ๊ฐ๋ฅ์ฑ์ ๋์ผ ์ ์์ต๋๋ค.
1. Program.cs ํ์ผ ์ค์
Program.cs ํ์ผ์์ ์๋น์ค์ DbContext๋ฅผ DI ์ปจํ ์ด๋์ ๋ฑ๋กํด์ผ ํฉ๋๋ค. ์ด๋ฅผ ์ํด CreateHostBuilder ๋ฉ์๋์์ ํ์ํ ์๋น์ค๋ฅผ ์ถ๊ฐํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.EntityFrameworkCore;
using WindowsFormsApp1Core.SQLModels;
using Syncfusion.Blazor;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using WindowsFormsApp1Core.WorkManagementSystem.WMSrvs;
namespace WorkManagementSystem
{
static class Program
{
[STAThread]
static void Main(string[] args)
{
Application.SetHighDpiMode(HighDpiMode.SystemAware);
Application.SetCompatibleTextRenderingDefault(false);
var host = CreateHostBuilder(args).Build();
host.Start();
Application.Run(new FormMain());
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder =>
{
webBuilder.ConfigureServices((context, services) =>
{
services.AddDbContext<AppDbContext>(options =>
options.UseMySql(
context.Configuration.GetConnectionString("DefaultConnection"),
ServerVersion.AutoDetect(context.Configuration.GetConnectionString("DefaultConnection"))
));
services.AddScoped<WorkInstructionService>();
services.AddControllersWithViews();
services.AddRazorPages();
services.AddServerSideBlazor();
services.AddSyncfusionBlazor();
});
webBuilder.Configure((context, app) =>
{
var env = context.HostingEnvironment;
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapBlazorHub();
endpoints.MapRazorPages();
endpoints.MapFallbackToPage("/_Host");
});
});
});
}
}
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ์ค์
2. appsettings.json ํ์ผ ์ค์
๋ฐ์ดํฐ๋ฒ ์ด์ค ์ฐ๊ฒฐ ๋ฌธ์์ด์ appsettings.json ํ์ผ์ ์ ์ฅ๋ฉ๋๋ค. ์ด๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฐ๊ฒฐ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
json์ฝ๋ ๋ณต์ฌ
{
"ConnectionStrings": {
"DefaultConnection": "Server=localhost;Database=plc_data_model1;User=root;Password=Alejd1785!;"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
},
"AllowedHosts": "*"
}
DbContext ์ค์
3. AppDbContext ํด๋์ค ์ค์
AppDbContext ํด๋์ค๋ Entity Framework Core๋ฅผ ์ฌ์ฉํ์ฌ ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ๋ ํด๋์ค์ ๋๋ค. ์ฌ๊ธฐ์๋ ํ์ํ DbSet์ ์ ์ํ๊ณ , ์ฐ๊ฒฐ ๋ฌธ์์ด์ ์ค์ ํฉ๋๋ค.
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.EntityFrameworkCore;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
namespace WindowsFormsApp1Core.SQLModels
{
public class AppDbContext : DbContext
{
public AppDbContext(DbContextOptions<AppDbContext> options) : base(options) { }
public DbSet<PlcDataModel> PlcDatas { get; set; }
public DbSet<Table1> Table1s { get; set; }
public DbSet<Table2> Table2s { get; set; }
public DbSet<WorkInstruction> WorkInstructions { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
if (!optionsBuilder.IsConfigured)
{
string connectionString = "Server=localhost;Database=plc_data_model1;User=root;Password=Alejd1785!;";
optionsBuilder.UseMySql(connectionString, ServerVersion.AutoDetect(connectionString));
}
}
}
}
๊ฒฐ๋ก
์ด๋ฒ ํฌ์คํ ์์๋ **WorkInstructionService**์ **AppDbContext**๋ฅผ DI ์ปจํ ์ด๋์ ๋ฑ๋กํ๊ณ , ๋ฐ์ดํฐ๋ฒ ์ด์ค์์ ์ฐ๊ฒฐ์ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃจ์์ต๋๋ค. ์ด๋ฅผ ํตํด ์๋น์ค์ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๊ฐ์ ์ํธ ์์ฉ์ ์ํํ๊ฒ ํ์์ผ๋ฉฐ, ์์กด์ฑ ์ฃผ์ ์ ํตํด ์ฝ๋์ ๋ชจ๋ํ์ ํ ์คํธ ๊ฐ๋ฅ์ฑ์ ๋์์ต๋๋ค. ๋ค์ ํฌ์คํ ์์๋ ํ๋ก์ ํธ์ ์ ๋ ํ ์คํธ์ ๋๋ฒ๊น ๋ฐฉ๋ฒ์ ๋ํด ๋ค๋ฃฐ ์์ ์ ๋๋ค.
9. ์๋น์ค ๋ฐ ์ปจํธ๋กค๋ฌ ๊ตฌํ
์๋ก : ์ด๋ฒ ํฌ์คํ ์์๋ **WorkInstructionService**์ **WorkInstructionController**์ ๊ตฌ์ฒด์ ์ธ ๊ตฌํ ๋ฐฉ๋ฒ์ ๋ค๋ฃจ๊ฒ ์ต๋๋ค. ์ด๋ฅผ ํตํด ์์ ์ง์์๋ฅผ ์์ฑ, ์ฝ๊ธฐ, ์ ๋ฐ์ดํธ, ์ญ์ (CRUD)ํ๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๊ณ , ๊ฐ ๋ฉ์๋๊ฐ ์ด๋ป๊ฒ ๋์ํ๋์ง ์ค๋ช ํ๊ฒ ์ต๋๋ค.
์๋น์ค ๊ตฌํ
WorkInstructionService ํด๋์ค๋ **AppDbContext**๋ฅผ ํตํด ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ํธ ์์ฉํ๋ฉฐ, ์์ ์ง์์์ CRUD ์์ ์ ์ฒ๋ฆฌํฉ๋๋ค. ์ด ํด๋์ค๋ ์ปจํธ๋กค๋ฌ์์ ํธ์ถ๋์ด ์์ ์ง์์ ๊ด๋ จ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค.
1. WorkInstructionService ํด๋์ค
csharp์ฝ๋ ๋ณต์ฌ
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using WindowsFormsApp1Core.SQLModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.WorkManagementSystem.WMSrvs
{
public class WorkInstructionService
{
private readonly AppDbContext _context;
private readonly ILogger<WorkInstructionService> _logger;
public WorkInstructionService(AppDbContext context, ILogger<WorkInstructionService> logger)
{
_context = context;
_logger = logger;
}
public async Task<IEnumerable<WorkInstruction>> GetWorkInstructionsAsync()
{
try
{
return await _context.WorkInstructions.ToListAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
throw;
}
}
public async Task AddWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
_context.WorkInstructions.Add(workInstruction);
await _context.SaveChangesAsync();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
throw;
}
}
public async Task UpdateWorkInstructionAsync(WorkInstruction workInstruction)
{
try
{
var existing = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == workInstruction.TaskName);
if (existing != null)
{
existing.Content = workInstruction.Content;
existing.Date = workInstruction.Date;
existing.Writer = workInstruction.Writer;
existing.Priority = workInstruction.Priority;
existing.IsCompleted = workInstruction.IsCompleted;
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", workInstruction.TaskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
throw;
}
}
public async Task DeleteWorkInstructionAsync(string taskName)
{
try
{
var workInstruction = await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
if (workInstruction != null)
{
_context.WorkInstructions.Remove(workInstruction);
await _context.SaveChangesAsync();
}
else
{
_logger.LogWarning("Work instruction not found: {TaskName}", taskName);
}
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
throw;
}
}
public async Task<WorkInstruction> GetWorkInstructionByTaskNameAsync(string taskName)
{
try
{
return await _context.WorkInstructions.FirstOrDefaultAsync(w => w.TaskName == taskName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instruction by task name");
throw;
}
}
}
}
์ปจํธ๋กค๋ฌ ๊ตฌํ
WorkInstructionController ํด๋์ค๋ API ์๋ํฌ์ธํธ๋ฅผ ์ ๊ณตํ๋ฉฐ, ์๋น์ค ํด๋์ค๋ฅผ ํตํด ์์ ์ง์์์ CRUD ์์ ์ ์ฒ๋ฆฌํฉ๋๋ค. ๊ฐ ๋ฉ์๋๋ HTTP ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ , ์ ์ ํ ์๋ต์ ๋ฐํํฉ๋๋ค.
2. WorkInstructionController ํด๋์ค
csharp์ฝ๋ ๋ณต์ฌ
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Threading.Tasks;
using WindowsFormsApp1Core.WorkManagementSystem.WMModels;
using WindowsFormsApp1Core.WorkManagementSystem.WMSrvs;
using Microsoft.Extensions.Logging;
namespace WindowsFormsApp1Core.Dashboard.Controllers
{
[ApiController]
[Route("api/[controller]")]
public class WorkInstructionController : ControllerBase
{
private readonly WorkInstructionService _service;
private readonly ILogger<WorkInstructionController> _logger;
public WorkInstructionController(WorkInstructionService service, ILogger<WorkInstructionController> logger)
{
_service = service;
_logger = logger;
}
[HttpGet]
public async Task<ActionResult<IEnumerable<WorkInstruction>>> Get()
{
try
{
var workInstructions = await _service.GetWorkInstructionsAsync();
return Ok(workInstructions);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error fetching work instructions");
return StatusCode(500, "Internal server error");
}
}
[HttpPost]
public async Task<ActionResult<WorkInstruction>> Post(WorkInstruction workInstruction)
{
try
{
await _service.AddWorkInstructionAsync(workInstruction);
return CreatedAtAction(nameof(Get), new { id = workInstruction.TaskName }, workInstruction);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error adding work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpPut("{taskName}")]
public async Task<IActionResult> Put(string taskName, WorkInstruction workInstruction)
{
try
{
var existingWorkInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (existingWorkInstruction == null)
{
return NotFound();
}
await _service.UpdateWorkInstructionAsync(workInstruction);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error updating work instruction");
return StatusCode(500, "Internal server error");
}
}
[HttpDelete("{taskName}")]
public async Task<IActionResult> Delete(string taskName)
{
try
{
var workInstruction = await _service.GetWorkInstructionByTaskNameAsync(taskName);
if (workInstruction == null)
{
return NotFound();
}
await _service.DeleteWorkInstructionAsync(taskName);
return NoContent();
}
catch (Exception ex)
{
_logger.LogError(ex, "Error deleting work instruction");
return StatusCode(500, "Internal server error");
}
}
}
}
๊ฒฐ๋ก
์ด๋ฒ ํฌ์คํ ์์๋ **WorkInstructionService**์ **WorkInstructionController**์ ๊ตฌํ์ ํตํด ์์ ์ง์์์ CRUD ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ ๋ฐฉ๋ฒ์ ๋ค๋ฃจ์์ต๋๋ค. ์ด๋ฅผ ํตํด ํ๋ก์ ํธ์ ํต์ฌ ๊ธฐ๋ฅ์ ๊ตฌํํ๊ณ , ์ฌ๋ฐ๋ฅธ DI ์ค์ ๊ณผ ์๋น์ค-์ปจํธ๋กค๋ฌ ๊ตฌ์กฐ๋ฅผ ์ดํดํ ์ ์์์ต๋๋ค. ๋ค์ ํฌ์คํ ์์๋ ํ๋ก์ ํธ์ ์ ๋ ํ ์คํธ์ ๋๋ฒ๊น ๋ฐฉ๋ฒ์ ๋ํด ๋ค๋ฃฐ ์์ ์ ๋๋ค.