|
| 1 | +namespace Invoices_Manager_API.Controllers.v01 |
| 2 | +{ |
| 3 | + [ApiController] |
| 4 | + [TypeFilter(typeof(AuthFilter))] |
| 5 | + [Route("api/v01/[controller]")] |
| 6 | + public class BackUpController : ControllerBase |
| 7 | + { |
| 8 | + private readonly ILogger<BackUpController> _logger; |
| 9 | + private readonly DataBaseContext _db; |
| 10 | + |
| 11 | + public BackUpController(ILogger<BackUpController> logger, DataBaseContext db) |
| 12 | + { |
| 13 | + _logger = logger; |
| 14 | + _db = db; |
| 15 | + } |
| 16 | + |
| 17 | + private async Task<UserModel?> GetCurrentUser() |
| 18 | + { |
| 19 | + // Get the bearer token from the header |
| 20 | + var bearerToken = HttpContext.Request.Headers["bearerToken"].ToString(); |
| 21 | + var user = await UserCore.GetCurrentUser(_db, bearerToken); |
| 22 | + return user; |
| 23 | + } |
| 24 | + |
| 25 | + [HttpGet] |
| 26 | + [Route("GetAll")] |
| 27 | + public async Task<IActionResult> GetAll() |
| 28 | + { |
| 29 | + // not implemented yet |
| 30 | + return await Task.FromResult(new StatusCodeResult(501)); |
| 31 | + |
| 32 | + //get the user |
| 33 | + var user = await GetCurrentUser(); |
| 34 | + if (user == null) |
| 35 | + return BadRequest("The user is null"); |
| 36 | + |
| 37 | + //get all back ups |
| 38 | + var backUps = user.BackUpInfos.ToList(); |
| 39 | + |
| 40 | + //return all back ups |
| 41 | + return Ok(backUps); |
| 42 | + } |
| 43 | + |
| 44 | + [HttpGet] |
| 45 | + public async Task<IActionResult> Get(int id) |
| 46 | + { |
| 47 | + // not implemented yet |
| 48 | + return await Task.FromResult(new StatusCodeResult(501)); |
| 49 | + |
| 50 | + //get the user |
| 51 | + var user = await GetCurrentUser(); |
| 52 | + if (user == null) |
| 53 | + return BadRequest("The user is null"); |
| 54 | + |
| 55 | + //check if there is an id |
| 56 | + if (id == 0 || id < 0) |
| 57 | + return BadRequest("The id is not valid"); |
| 58 | + |
| 59 | + //get the back up |
| 60 | + var backUp = user.BackUpInfos.Find(x => x.Id == id); |
| 61 | + |
| 62 | + //check if the back up exist |
| 63 | + if (backUp is null) |
| 64 | + return NotFound($"The back up with the id '{id}' does not exist"); |
| 65 | + |
| 66 | + //return the back up |
| 67 | + return Ok(backUp); |
| 68 | + } |
| 69 | + |
| 70 | + [HttpGet] |
| 71 | + [Route("Download")] |
| 72 | + public async Task<IActionResult> Download(int id) |
| 73 | + { |
| 74 | + // not implemented yet |
| 75 | + return await Task.FromResult(new StatusCodeResult(501)); |
| 76 | + |
| 77 | + //get the user |
| 78 | + var user = await GetCurrentUser(); |
| 79 | + if (user == null) |
| 80 | + return BadRequest("The user is null"); |
| 81 | + |
| 82 | + //check if there is an id |
| 83 | + if (id == 0 || id < 0) |
| 84 | + return BadRequest("The id is not valid"); |
| 85 | + |
| 86 | + //get the back up |
| 87 | + var backUp = user.BackUpInfos.Find(x => x.Id == id); |
| 88 | + |
| 89 | + //check if the back up exist |
| 90 | + if (backUp is null) |
| 91 | + return NotFound($"The back up with the id '{id}' does not exist"); |
| 92 | + |
| 93 | + //get the file |
| 94 | + string filePath = FileCore.GetBackUpFilePath(backUp.FileID, user); |
| 95 | + |
| 96 | + //check if the file exist |
| 97 | + if (filePath is null) |
| 98 | + return NotFound($"The file with the id '{id}' does not exist"); |
| 99 | + |
| 100 | + //open a filestream for the chunking |
| 101 | + var fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read); |
| 102 | + return new FileStreamResult(fileStream, "application/octet-stream") |
| 103 | + { |
| 104 | + FileDownloadName = Path.GetFileName(filePath), |
| 105 | + EnableRangeProcessing = true |
| 106 | + }; |
| 107 | + } |
| 108 | + |
| 109 | + [HttpPost] |
| 110 | + [Route("Upload")] |
| 111 | + public async Task<IActionResult> Upload() |
| 112 | + { |
| 113 | + // not implemented yet |
| 114 | + return await Task.FromResult(new StatusCodeResult(501)); |
| 115 | + } |
| 116 | + |
| 117 | + [HttpPost] |
| 118 | + [Route("GenerateBackUp")] |
| 119 | + public async Task<IActionResult> GenerateBackUp() |
| 120 | + { |
| 121 | + // not implemented yet |
| 122 | + return await Task.FromResult(new StatusCodeResult(501)); |
| 123 | + |
| 124 | + //get the user |
| 125 | + var user = await GetCurrentUser(); |
| 126 | + if (user == null) |
| 127 | + return BadRequest("The user is null"); |
| 128 | + |
| 129 | + //check if the user is already in the queue |
| 130 | + if (BackUpCore.IsUserInQueue(user)) |
| 131 | + return Conflict("You are already in the queue"); |
| 132 | + |
| 133 | + int place = BackUpCore.AddUserToQueue(user); |
| 134 | + |
| 135 | + //return the back up |
| 136 | + return Ok($"You place in the Queue '{place}'"); |
| 137 | + } |
| 138 | + |
| 139 | + [HttpGet] |
| 140 | + [Route("QueuePlace")] |
| 141 | + public async Task<IActionResult> QueuePlace() |
| 142 | + { |
| 143 | + // not implemented yet |
| 144 | + return await Task.FromResult(new StatusCodeResult(501)); |
| 145 | + |
| 146 | + //get the user |
| 147 | + var user = await GetCurrentUser(); |
| 148 | + if (user == null) |
| 149 | + return BadRequest("The user is null"); |
| 150 | + |
| 151 | + //check if the user is already in the queue |
| 152 | + if (!BackUpCore.IsUserInQueue(user)) |
| 153 | + return Conflict("You are not in the queue"); |
| 154 | + |
| 155 | + int place = BackUpCore.GetQueuePlace(user); |
| 156 | + |
| 157 | + //return the back up |
| 158 | + return Ok($"You place in the Queue '{place}'"); |
| 159 | + } |
| 160 | + |
| 161 | + [HttpDelete] |
| 162 | + public async Task<IActionResult> Delete(int id) |
| 163 | + { |
| 164 | + // not implemented yet |
| 165 | + return await Task.FromResult(new StatusCodeResult(501)); |
| 166 | + |
| 167 | + //get the user |
| 168 | + var user = await GetCurrentUser(); |
| 169 | + if (user == null) |
| 170 | + return BadRequest("The user is null"); |
| 171 | + |
| 172 | + //check if there is an id |
| 173 | + if (id == 0 || id < 0) |
| 174 | + return BadRequest("The id is not valid"); |
| 175 | + |
| 176 | + //get the back up |
| 177 | + var backUp = user.BackUpInfos.Find(x => x.Id == id); |
| 178 | + |
| 179 | + //check if the back up exist |
| 180 | + if (backUp is null) |
| 181 | + return NotFound($"The back up with the id '{id}' does not exist"); |
| 182 | + |
| 183 | + //remove the back up from the db |
| 184 | + _db.BackUpInfo.Remove(backUp); |
| 185 | + |
| 186 | + //remove the back up from the user |
| 187 | + user.BackUpInfos.Remove(backUp); |
| 188 | + |
| 189 | + //save the changes |
| 190 | + await _db.SaveChangesAsync(); |
| 191 | + |
| 192 | + //return the back up |
| 193 | + return Ok(backUp); |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments