aboutsummaryrefslogtreecommitdiff
path: root/Backend/Api/Api/Controllers/LocationController.cs
blob: c9ef9ba9fd82e7d1770c2ce745e0847e9fba428c (plain) (blame)
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
using Api.Interfaces;
using Api.Models;
using Microsoft.AspNetCore.Authorization;
using System.Data;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Api.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class LocationController : ControllerBase
    {
        private readonly ILocationService _locationService;
        public LocationController(ILocationService locationService)
        {
            _locationService = locationService;
        }

        [HttpPost("add")]
        [Authorize(Roles = "User")]
        public async Task<ActionResult<Location>> addPost([FromBody] Location loc)
        {
            var res = await _locationService.add(loc);
            if (res != null)
            {
                return Ok(res);
            }
            return BadRequest();
        }
        [HttpGet]
        [Authorize(Roles = "User")]
        public async Task<ActionResult<List<Location>>> getAllPosts()
        {
            var res = await _locationService.getAllLocation();
            if (res != null)
            {
                return Ok(res);
            }
            return BadRequest();
        }
        [HttpGet("loc /{id}")]
        [Authorize(Roles = "User")]
        public async Task<ActionResult<Location>> getLocationByid(string id)
        {
            var res = await _locationService.getById(id);
            if (res != null)
            {
                return Ok(res);
            }
            return BadRequest();
        }

        [HttpPost("search")]
        [Authorize(Roles = "User")]
        public async Task<ActionResult<List<Location>>> searchLocation(int searchtype ,string? query,Coords? coords)
        {
            List<Location> ret = new List<Location>();
            switch (searchtype)
            {
                case 1:
                    ret = await _locationService.SearchLocation(query);
                    return Ok(ret);
                case 2:
                    ret = await _locationService.SearchLocation(coords);
                    return Ok(ret);
                default:
                    ret = await _locationService.SearchLocation(query);
                    return Ok(ret);
            }
        }
    }
}