Skip to content

Commit f8f94a3

Browse files
committed
add delete method for all controllers
1 parent 5465922 commit f8f94a3

3 files changed

Lines changed: 97 additions & 3 deletions

File tree

Controllers/LastOpenedDocumentsController.cs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,34 @@ public IActionResult InsertNewLastOpenedDocument([FromBody]LastOpenedDocument la
105105
}
106106
}
107107
}
108+
109+
[HttpDelete("[action]/{lastOpenedDocumentId}")]
110+
public IActionResult DeleteLastOpenedDocument(int lastOpenedDocumentId)
111+
{
112+
using (var transaction = _context.Database.BeginTransaction())
113+
{
114+
try
115+
{
116+
var document = _context.Medicines.FirstOrDefault(m => m.MedicineId == lastOpenedDocumentId);
117+
118+
if (document == null)
119+
{
120+
return NotFound();
121+
}
122+
123+
_context.Medicines.Remove(document);
124+
_context.SaveChanges();
125+
126+
transaction.Commit();
127+
128+
return new NoContentResult();
129+
}
130+
catch (Exception e)
131+
{
132+
_logger.LogError(e, "Can not delete last opened document");
133+
return StatusCode(StatusCodes.Status500InternalServerError);
134+
}
135+
}
136+
}
108137
}
109138
}

Controllers/MedicineController.cs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using MedHelp.Data;
45
using MedHelp.Models;
6+
using Microsoft.AspNetCore.Http;
57
using Microsoft.AspNetCore.Mvc;
68
using Microsoft.EntityFrameworkCore.Extensions.Internal;
79
using Microsoft.Extensions.Logging;
@@ -27,5 +29,34 @@ public IEnumerable<string> GetMedicines()
2729
{
2830
return _context.Medicines.ToList().Select(m => m.MedicineName).ToList();
2931
}
32+
33+
[HttpDelete("[action]/{medicineId}")]
34+
public IActionResult DeleteMedicine(int medicineId)
35+
{
36+
using (var transaction = _context.Database.BeginTransaction())
37+
{
38+
try
39+
{
40+
var medicine = _context.Medicines.FirstOrDefault(m => m.MedicineId == medicineId);
41+
42+
if (medicine == null)
43+
{
44+
return NotFound();
45+
}
46+
47+
_context.Medicines.Remove(medicine);
48+
_context.SaveChanges();
49+
50+
transaction.Commit();
51+
52+
return new NoContentResult();
53+
}
54+
catch (Exception e)
55+
{
56+
_logger.LogError(e, "Can not delete medicine");
57+
return StatusCode(StatusCodes.Status500InternalServerError);
58+
}
59+
}
60+
}
3061
}
3162
}

Controllers/TemplatesController.cs

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Linq;
34
using MedHelp.Data;
45
using MedHelp.Models;
6+
using Microsoft.AspNetCore.Http;
57
using Microsoft.AspNetCore.Mvc;
68
using Microsoft.EntityFrameworkCore;
9+
using Microsoft.Extensions.Logging;
710

811
namespace MedHelp.Controllers
912
{
@@ -13,10 +16,12 @@ namespace MedHelp.Controllers
1316
public class TemplatesController : Controller
1417
{
1518
private readonly MedHelpContext _context;
19+
private readonly ILogger _logger;
1620

17-
public TemplatesController(MedHelpContext context)
21+
public TemplatesController(MedHelpContext context, ILogger<TemplatesController> logger)
1822
{
1923
_context = context;
24+
_logger = logger;
2025
}
2126

2227
[HttpGet("[action]/{format?}")]
@@ -31,5 +36,34 @@ public Template GetTemplateWithProperties(int templateId)
3136
var template = _context.Templates.Single(t => t.TemplateId == templateId);
3237
return template;
3338
}
39+
40+
[HttpDelete("[action]/{templateId}")]
41+
public IActionResult DeleteTemplate(int templateId)
42+
{
43+
using (var transaction = _context.Database.BeginTransaction())
44+
{
45+
try
46+
{
47+
var template = _context.Medicines.FirstOrDefault(m => m.MedicineId == templateId);
48+
49+
if (template == null)
50+
{
51+
return NotFound();
52+
}
53+
54+
_context.Medicines.Remove(template);
55+
_context.SaveChanges();
56+
57+
transaction.Commit();
58+
59+
return new NoContentResult();
60+
}
61+
catch (Exception e)
62+
{
63+
_logger.LogError(e, "Can not delete template");
64+
return StatusCode(StatusCodes.Status500InternalServerError);
65+
}
66+
}
67+
}
3468
}
3569
}

0 commit comments

Comments
 (0)