Advertisement
drakon-firestone

Untitled

Apr 9th, 2025 (edited)
302
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  1. using Backend.Data;
  2. using Backend.Models;
  3. using Microsoft.AspNetCore.Mvc;
  4. using Microsoft.EntityFrameworkCore;
  5.  
  6.  
  7. namespace Backend.Controllers
  8. {
  9.  
  10.     [ApiController]
  11.     [Route("api/[controller]")]
  12.     public class UsersController : ControllerBase
  13.     {
  14.         private readonly ApplicationDbContext _context;
  15.  
  16.         public UsersController(ApplicationDbContext context)
  17.         {
  18.             _context = context;
  19.         }
  20.  
  21.         [HttpGet]
  22.         public async Task<ActionResult<IEnumerable<User>>> GetUsers()
  23.         {
  24.             var users = await _context.Users.ToListAsync();
  25.             return Ok(users);
  26.         }
  27.  
  28.         [HttpGet("{id}")]
  29.         public async Task<ActionResult<User>> GetUser(int id)
  30.         {
  31.             var user = await _context.Users.FindAsync(id);
  32.  
  33.             if(user == null)
  34.             {
  35.                 return NotFound();
  36.             }
  37.  
  38.             return Ok(user);
  39.         }
  40.  
  41.         [HttpPost]
  42.         public async Task<ActionResult<User>> PostUser(User user)
  43.         {
  44.             if (user == null)
  45.             {
  46.                 return BadRequest("User data is required");
  47.             }
  48.            
  49.             try
  50.             {
  51.                 _context.Users.Add(user);
  52.                 await _context.SaveChangesAsync();
  53.  
  54.                 return CreatedAtAction("GetUser", new {id = user.Id }, user);
  55.             }
  56.             catch (Exception ex)
  57.             {
  58.                 return StatusCode(500, "En error occured while creatint user. Try again later");
  59.             }
  60.         }
  61.  
  62.         [HttpPut("{id}")]
  63.         public async Task<IActionResult> PutUser(int id, User user)
  64.         {
  65.             if (id != user.Id)
  66.             {
  67.                 return BadRequest();
  68.             }
  69.            
  70.             _context.Entry(user).State = EntityState.Modified;
  71.            
  72.             try
  73.             {
  74.                 await _context.SaveChangesAsync();
  75.             }
  76.             catch (DbUpdateConcurrencyException)
  77.             {
  78.                 if (!UserExists(id))
  79.                 {
  80.                     return NotFound();
  81.                 }
  82.                 else
  83.                 {
  84.                     throw;
  85.                 }
  86.             }
  87.             return NoContent();
  88.         }
  89.        
  90.         private bool UserExists(int id)
  91.         {
  92.             return _context.Users.Any(e => e.Id == id);
  93.         }
  94.  
  95.     }
  96.  
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement