diff options
| author | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-10-22 21:26:01 +0200 |
|---|---|---|
| committer | Paweł Bernaciak <pawelbernaciak@zohomail.eu> | 2023-10-22 21:26:01 +0200 |
| commit | 68e4282555a55f429320b80f09f609970dc76e92 (patch) | |
| tree | d208c6bcc6651dbe57fb54bdcf5cf061d00d922e | |
| parent | 6900a7fe2e03ad777bf40fc28baf63f2c383a2ae (diff) | |
Finish controllers
| -rw-r--r-- | backend/Elements.Backend/Controllers/AuthController.cs | 4 | ||||
| -rw-r--r-- | backend/Elements.Backend/Controllers/SuggestionController.cs | 148 |
2 files changed, 149 insertions, 3 deletions
diff --git a/backend/Elements.Backend/Controllers/AuthController.cs b/backend/Elements.Backend/Controllers/AuthController.cs index fd388b8..e9dae00 100644 --- a/backend/Elements.Backend/Controllers/AuthController.cs +++ b/backend/Elements.Backend/Controllers/AuthController.cs @@ -49,9 +49,7 @@ public class AuthController : ControllerBase user = new User() { Name = payload.Name, - GoogleId = payload.Subject, - Elements = new List<Element>(), - Suggestions = new List<Suggestion>() + GoogleId = payload.Subject }; await _dbContext.Users.AddAsync(user); diff --git a/backend/Elements.Backend/Controllers/SuggestionController.cs b/backend/Elements.Backend/Controllers/SuggestionController.cs new file mode 100644 index 0000000..77a3944 --- /dev/null +++ b/backend/Elements.Backend/Controllers/SuggestionController.cs @@ -0,0 +1,148 @@ +using System.Security.Claims; +using System.Text.Json; +using Elements.Data; +using Elements.Data.Models; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Mvc; +using Microsoft.EntityFrameworkCore; +using SixLabors.ImageSharp; +using SixLabors.ImageSharp.Formats.Png; +using SixLabors.ImageSharp.PixelFormats; + +namespace Elements.Backend.Controllers; + +[ApiController] +public class SuggestionController : ControllerBase +{ + private readonly ApplicationDbContext _dbContext; + + public SuggestionController(ApplicationDbContext dbContext) + { + _dbContext = dbContext; + } + + [Route("/suggestion/search")] + [HttpGet] + public async Task<IActionResult> GetSuggestions(int firstElementId, int secondElementId) + { + bool firstExists = await _dbContext.Elements.AnyAsync(e => e.Id == firstElementId); + bool secondExists = await _dbContext.Elements.AnyAsync(e => e.Id == secondElementId); + if (!firstExists || !secondExists) + return BadRequest(); + + IEnumerable<Suggestion> suggestion = await _dbContext.Suggestions + .Where(s => + (s.FirstElementId == firstElementId && s.SecondElementId == secondElementId) || + (s.FirstElementId == secondElementId && s.SecondElementId == firstElementId)) + .Include(suggestion => suggestion.Votes) + .ToListAsync(); + if (!suggestion.Any()) + return NotFound(); + + var result = suggestion.Select(s => new + { + s.Id, + s.Name, + Icon = Convert.ToBase64String(s.Icon), + s.FirstElementId, + s.SecondElementId, + Votes = s.Votes.Count, + s.UserId, + s.VotingEnd + }).ToList(); + + var serializeOptions = new JsonSerializerOptions { PropertyNamingPolicy = JsonNamingPolicy.CamelCase }; + return Ok(JsonSerializer.Serialize(result, serializeOptions)); + } + + public class SuggestionVoteModel + { + public required int Id { get; set; } + } + + [Route("/suggestion/vote")] + [Authorize] + [HttpPost] + public async Task<IActionResult> PostVote([FromBody] SuggestionVoteModel vote) + { + Suggestion? suggestion = await _dbContext.Suggestions + .Include(s => s.Votes) + .FirstOrDefaultAsync(s => s.Id == vote.Id); + if (suggestion == null) + return NotFound(); + + IEnumerable<Claim> claims = User.Claims; + string? currentUserId = claims.FirstOrDefault(claim => claim.Type == "id")?.Value; + if (currentUserId == null) + return StatusCode(StatusCodes.Status500InternalServerError); + //Check if user is voting for own element + if (suggestion.UserId.ToString() == currentUserId) + return BadRequest(); + //Check if user already voted for this element + IEnumerable<Vote> suggestionVotes = suggestion.Votes; + if (suggestionVotes.Any(s => s.UserId.ToString() == currentUserId)) + return BadRequest(); + + Vote newVote = new() + { + UserId = int.Parse(currentUserId), + SuggestionId = suggestion.Id + }; + await _dbContext.Votes.AddAsync(newVote); + await _dbContext.SaveChangesAsync(); + + return Ok(); + } + + public class SuggestionCreateModel + { + public required string Name { get; set; } + public required string IconBitmap { get; set; } + public required int FirstElementId { get; set; } + public required int SecondElementId { get; set; } + } + + [Route("/suggestion/create")] + [Authorize] + [HttpPost] + public async Task<IActionResult> PostSuggestion([FromBody] SuggestionCreateModel suggestion) + { + IEnumerable<Claim> claims = User.Claims; + string? currentUserId = claims.FirstOrDefault(claim => claim.Type == "id")?.Value; + if (currentUserId == null) + return StatusCode(StatusCodes.Status500InternalServerError); + + if (await _dbContext.Suggestions.AnyAsync(s => s.UserId.ToString() == currentUserId)) + return BadRequest(); + + Suggestion newSuggestion = new() + { + Name = suggestion.Name, + Icon = ConvertBitmapToPng(Convert.FromBase64String(suggestion.IconBitmap)), + FirstElementId = suggestion.FirstElementId, + SecondElementId = suggestion.SecondElementId, + VotingEnd = DateTime.UtcNow + TimeSpan.FromMinutes(1), + UserId = int.Parse(currentUserId) + }; + await _dbContext.Suggestions.AddAsync(newSuggestion); + await _dbContext.SaveChangesAsync(); + + Vote newVote = new() + { + UserId = int.Parse(currentUserId), + SuggestionId = newSuggestion.Id + }; + await _dbContext.Votes.AddAsync(newVote); + await _dbContext.SaveChangesAsync(); + + return Ok(); + } + + private static byte[] ConvertBitmapToPng(byte[] bitmapData) + { + Image<Argb32> image = Image.LoadPixelData<Argb32>(bitmapData, 16, 16); + using MemoryStream resultStream = new(); + image.Save(resultStream, new PngEncoder()); + return resultStream.ToArray(); + } +}
\ No newline at end of file |
