-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReservationController.cs
115 lines (96 loc) · 4.08 KB
/
ReservationController.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using SANTSG.Web.Models;
namespace SANTSG.Web.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ReservationController : ControllerBase
{
private readonly IWebHostEnvironment _hostingEnvironment;
public ReservationController(IWebHostEnvironment hostingEnvironment)
{
_hostingEnvironment = hostingEnvironment;
}
// GET: api/reservation
[HttpGet]
public IEnumerable<RezervationCard> Get()
{
List<RezervationCard> rezervationCards = new List<RezervationCard>();
var rezervationCard = new RezervationCard
{
Operator = new TourOperator("AMOR", "AMOR REISE GMBH"),
VoucherNo = 1
};
rezervationCards.Add(rezervationCard);
return rezervationCards.ToList();
}
// GET api/reservation/5
[HttpGet("{id}", Name = nameof(GetReservation))]
public IActionResult GetReservation(string id)
{
return new OkObjectResult($"{id}");
}
// POST api/values
[HttpPost]
public IActionResult Post([FromBody]object reservation)
{
if (reservation == null)
{
return BadRequest("Lütfen POST istegini method body içerisinde gönderiniz.");
}
//TODO: Projenin klasör dizinini al
string webRootPath = _hostingEnvironment.WebRootPath;
string contentRootPath = _hostingEnvironment.ContentRootPath;
string fileName = @"reservationFile.txt";
string reservationFilePath = Path.Combine(contentRootPath, "reservationFiles");
string combinePath = Path.Combine(reservationFilePath, fileName);
//TODO: reservationFiles adında bir klasör var mı kontrol et!
bool exists = System.IO.Directory.Exists(reservationFilePath);
//TODO: Klasör Yok ise oluştur - Oluşturulamıyorsa hata dön!
if (!exists)
System.IO.Directory.CreateDirectory(reservationFilePath);
//TODO: Klasör oluşturuldu ise içine dosyası reservationFile.txt oluştur!
try
{
using (StreamWriter stream = new FileInfo(combinePath).AppendText())
{
Log(this.ControllerContext.RouteData.Values["controller"].ToString(),
this.ControllerContext.RouteData.Values["action"].ToString(),
this.ControllerContext.HttpContext.Request.Method,
reservation,
stream);
}
}
catch (UnauthorizedAccessException ex)
{
return BadRequest(ex);
}
return CreatedAtRoute(nameof(GetReservation), new { id = 1 }, new {
contoller = this.ControllerContext.RouteData.Values["controller"].ToString(),
action = this.ControllerContext.RouteData.Values["action"].ToString(),
requestMethod = this.ControllerContext.HttpContext.Request.Method,
json = reservation,
success = true
});
}
public static void Log(string controller,string action, string requestMethod, object message, StreamWriter sw)
{
sw.WriteLine("---------------------------------------");
sw.WriteLine($"{DateTime.Now.ToLongTimeString()} - {DateTime.Now.ToLongDateString()}");
sw.WriteLine($"Controller: {controller}");
sw.WriteLine($"Action: {action}");
sw.WriteLine($"Request Method: {requestMethod}");
sw.WriteLine("---------------------------------------");
sw.WriteLine(message);
sw.WriteLine("---------------------------------------");
sw.Flush();
}
}
}