File size: 5,646 Bytes
70556b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3be30d5
70556b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3be30d5
70556b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3be30d5
70556b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using Application.Abstractions.Interfaces;
using Application.DTOs.NodeDTOs;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace RobDeliveryAPI.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize]
    public class NodeController : ControllerBase
    {
        private readonly INodeService _nodeService;

        public NodeController(INodeService nodeService)
        {
            _nodeService = nodeService;
        }

        /// <summary>
        /// Create a new delivery node
        /// </summary>
        [HttpPost]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> CreateNode([FromBody] CreateNodeDTO nodeDto)
        {
            try
            {
                var result = await _nodeService.CreateNodeAsync(nodeDto);
                return CreatedAtAction(nameof(GetNodeById), new { id = result.Id }, result);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new { error = ex.Message });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = "An error occurred while creating the node", details = ex.Message });
            }
        }

        /// <summary>
        /// Get node by ID
        /// </summary>
        [HttpGet("{id}")]
        public async Task<IActionResult> GetNodeById(int id)
        {
            try
            {
                var node = await _nodeService.GetNodeByIdAsync(id);
                if (node == null)
                {
                    return NotFound(new { error = "Node not found" });
                }
                return Ok(node);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = "An error occurred while retrieving the node", details = ex.Message });
            }
        }

        /// <summary>
        /// Get all nodes
        /// </summary>
        [HttpGet]
        public async Task<IActionResult> GetAllNodes()
        {
            try
            {
                var nodes = await _nodeService.GetAllNodesAsync();
                return Ok(nodes);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = "An error occurred while retrieving nodes", details = ex.Message });
            }
        }

        /// <summary>
        /// Get nodes by type
        /// </summary>
        [HttpGet("type/{type}")]
        public async Task<IActionResult> GetNodesByType(NodeType type)
        {
            try
            {
                var nodes = await _nodeService.GetNodesByTypeAsync(type);
                return Ok(nodes);
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = "An error occurred while retrieving nodes by type", details = ex.Message });
            }
        }

        /// <summary>
        /// Find nearest node to given coordinates
        /// </summary>
        [HttpGet("nearest")]
        public async Task<IActionResult> FindNearestNode([FromQuery] double latitude, [FromQuery] double longitude, [FromQuery] NodeType? type = null)
        {
            try
            {
                var node = await _nodeService.FindNearestNodeAsync(latitude, longitude, type);
                if (node == null)
                {
                    return NotFound(new { error = "No nodes found" });
                }
                return Ok(node);
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new { error = ex.Message });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = "An error occurred while finding nearest node", details = ex.Message });
            }
        }

        /// <summary>
        /// Update a node
        /// </summary>
        [HttpPut("{id}")]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> UpdateNode(int id, [FromBody] UpdateNodeDTO nodeDto)
        {
            try
            {
                if (id != nodeDto.Id)
                {
                    return BadRequest(new { error = "ID mismatch" });
                }

                var result = await _nodeService.UpdateNodeAsync(nodeDto);
                if (!result)
                {
                    return NotFound(new { error = "Node not found" });
                }

                return Ok(new { message = "Node updated successfully" });
            }
            catch (ArgumentException ex)
            {
                return BadRequest(new { error = ex.Message });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = "An error occurred while updating the node", details = ex.Message });
            }
        }

        /// <summary>
        /// Delete a node
        /// </summary>
        [HttpDelete("{id}")]
        [Authorize(Roles = "Admin")]
        public async Task<IActionResult> DeleteNode(int id)
        {
            try
            {
                var result = await _nodeService.DeleteNodeAsync(id);
                if (!result)
                {
                    return NotFound(new { error = "Node not found" });
                }

                return Ok(new { message = "Node deleted successfully" });
            }
            catch (Exception ex)
            {
                return StatusCode(500, new { error = "An error occurred while deleting the node", details = ex.Message });
            }
        }
    }
}