From 7f77f0920c1e21189bcf1b6bd39b2aaef536c926 Mon Sep 17 00:00:00 2001 From: Oktawian-L Date: Sat, 9 Nov 2019 13:58:50 +0100 Subject: [PATCH] migration migration --- .../SubitonAPI/Controllers/AuthController.cs | 5 +- .../Controllers/PhotosController.cs | 57 ++++++++++++++----- .../Controllers/ProfileController.cs | 4 +- .../SubitonAPI/Controllers/UsersController.cs | 6 +- SubitonAPI/SubitonAPI/DTO/PhotoCreateDTO.cs | 11 ++++ SubitonAPI/SubitonAPI/GlobalSuppressions.cs | 6 ++ .../Migrations/20191017170121_extendPhotos.cs | 1 + .../Migrations/20191102214503_add_name.cs | 5 ++ 8 files changed, 75 insertions(+), 20 deletions(-) create mode 100644 SubitonAPI/SubitonAPI/DTO/PhotoCreateDTO.cs create mode 100644 SubitonAPI/SubitonAPI/GlobalSuppressions.cs diff --git a/SubitonAPI/SubitonAPI/Controllers/AuthController.cs b/SubitonAPI/SubitonAPI/Controllers/AuthController.cs index a0d165c..9c38cbd 100644 --- a/SubitonAPI/SubitonAPI/Controllers/AuthController.cs +++ b/SubitonAPI/SubitonAPI/Controllers/AuthController.cs @@ -39,8 +39,11 @@ public AuthController(IAuthRepository repository, IConfiguration configuration) public async Task Register(UserRegisterDTO userRegisterDTO) { + if (userRegisterDTO == null) + throw new ArgumentNullException(nameof(userRegisterDTO)); + userRegisterDTO.Username = userRegisterDTO.Username.ToLower(); - if (await _repository.UserExists(userRegisterDTO.Username)) + if (await _repository.UserExists(userRegisterDTO.Username).ConfigureAwait(false)) { return BadRequest("User already exixts"); } diff --git a/SubitonAPI/SubitonAPI/Controllers/PhotosController.cs b/SubitonAPI/SubitonAPI/Controllers/PhotosController.cs index 5b25920..72ea016 100644 --- a/SubitonAPI/SubitonAPI/Controllers/PhotosController.cs +++ b/SubitonAPI/SubitonAPI/Controllers/PhotosController.cs @@ -10,20 +10,44 @@ using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Options; using SubitonAPI.Data; +using SubitonAPI.DTO; using SubitonAPI.Helpers; using SubitonAPI.Models; namespace SubitonAPI.Controllers { + /// + /// Photo uploaded to claudinary + /// + /// [Authorize] [Route("api/[controller]")] [ApiController] public class PhotosController : ControllerBase { + /// + /// Database context + /// private readonly DataContext _context; + + /// + /// The user repository + /// private readonly IUserRepository _userRepository; + + /// + /// The mapper + /// private readonly IMapper _mapper; + + /// + /// The claudinary + /// private readonly IOptions _claudinarySettings; + + /// + /// The cloudinary + /// private readonly Cloudinary _cloudinary; public PhotosController(DataContext context, IUserRepository userRepository, IMapper mapper, IOptions claudinarySettings) @@ -42,6 +66,20 @@ public PhotosController(DataContext context, IUserRepository userRepository, IMa _cloudinary = new Cloudinary(account); } + // POST: api/Photos + [HttpPost] + public async Task AddPhotoForUser(int userId, PhotoCreateDTO photo) + { + _context.Photos.Add(photo); + await _context.SaveChangesAsync().ConfigureAwait(true); + + if (photo == null) + throw new ArgumentNullException(nameof(photo)); + + return CreatedAtAction("GetPhoto", new { id = photo.Id }, photo); + } + + // GET: api/Photos [HttpGet] public async Task>> GetPhotos() @@ -69,6 +107,10 @@ public async Task> GetPhoto(int id) [HttpPut("{id}")] public async Task PutPhoto(int id, Photo photo) { + + if (photo == null) + throw new ArgumentNullException(nameof(photo)); + if (id != photo.Id) { return BadRequest(); @@ -78,7 +120,7 @@ public async Task PutPhoto(int id, Photo photo) try { - await _context.SaveChangesAsync().ConfigureAwait(true); + await _context.SaveChangesAsync().ConfigureAwait(false); } catch (DbUpdateConcurrencyException) { @@ -95,17 +137,6 @@ public async Task PutPhoto(int id, Photo photo) return NoContent(); } - // POST: api/Photos - // To protect from overposting attacks, please enable the specific properties you want to bind to, for - // more details see https://aka.ms/RazorPagesCRUD. - [HttpPost] - public async Task> PostPhoto(Photo photo) - { - _context.Photos.Add(photo); - await _context.SaveChangesAsync().ConfigureAwait(true); - - return CreatedAtAction("GetPhoto", new { id = photo.Id }, photo); - } // DELETE: api/Photos/5 [HttpDelete("{id}")] @@ -118,7 +149,7 @@ public async Task> DeletePhoto(int id) } _context.Photos.Remove(photo); - await _context.SaveChangesAsync(); + await _context.SaveChangesAsync().ConfigureAwait(true); return photo; } diff --git a/SubitonAPI/SubitonAPI/Controllers/ProfileController.cs b/SubitonAPI/SubitonAPI/Controllers/ProfileController.cs index 60309b2..a220076 100644 --- a/SubitonAPI/SubitonAPI/Controllers/ProfileController.cs +++ b/SubitonAPI/SubitonAPI/Controllers/ProfileController.cs @@ -25,7 +25,7 @@ public ProfileController(DataContext context) [HttpGet] public async Task>> GetUsers() { - return await _context.Users.ToListAsync(); + return await _context.Users.ToListAsync().ConfigureAwait(false); } // GET: api/Users/5 @@ -97,7 +97,7 @@ public async Task> DeleteUser(int id) } _context.Users.Remove(user); - await _context.SaveChangesAsync(); + await _context.SaveChangesAsync().ConfigureAwait(false); return user; } diff --git a/SubitonAPI/SubitonAPI/Controllers/UsersController.cs b/SubitonAPI/SubitonAPI/Controllers/UsersController.cs index 503765a..aafbd7d 100644 --- a/SubitonAPI/SubitonAPI/Controllers/UsersController.cs +++ b/SubitonAPI/SubitonAPI/Controllers/UsersController.cs @@ -36,7 +36,7 @@ public UsersController(IUserRepository userRepository, IMapper mapper) [HttpGet] public async Task>> GetUsers() { - var users = await _userRepository.GetAllUsers(); + var users = await _userRepository.GetAllUsers().ConfigureAwait(false); // map from user to copllection dto var usersToReturn = _mapper.Map>(users); @@ -47,7 +47,7 @@ public async Task>> GetUsers() [HttpGet("{id}")] public async Task> GetUser(int id) { - var user = await _userRepository.GetUser(id); + var user = await _userRepository.GetUser(id).ConfigureAwait(false); if (user == null) { @@ -59,8 +59,6 @@ public async Task> GetUser(int id) // PUT: api/Users/5 - // To protect from overposting attacks, please enable the specific properties you want to bind to, for - // more details see https://aka.ms/RazorPagesCRUD. [HttpPut("{id}")] public async Task PutUser(int id, UserUpdateDTO userUpdate) { diff --git a/SubitonAPI/SubitonAPI/DTO/PhotoCreateDTO.cs b/SubitonAPI/SubitonAPI/DTO/PhotoCreateDTO.cs new file mode 100644 index 0000000..c23918a --- /dev/null +++ b/SubitonAPI/SubitonAPI/DTO/PhotoCreateDTO.cs @@ -0,0 +1,11 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace SubitonAPI.DTO +{ + public class PhotoCreateDTO + { + } +} diff --git a/SubitonAPI/SubitonAPI/GlobalSuppressions.cs b/SubitonAPI/SubitonAPI/GlobalSuppressions.cs new file mode 100644 index 0000000..0570b2f --- /dev/null +++ b/SubitonAPI/SubitonAPI/GlobalSuppressions.cs @@ -0,0 +1,6 @@ +// This file is used by Code Analysis to maintain SuppressMessage +// attributes that are applied to this project. +// Project-level suppressions either have no target or are given +// a specific target and scoped to a namespace, type, member, etc. + +[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "", Scope = "member", Target = "~M:SubitonAPI.Migrations.extendPhotos.Down(Microsoft.EntityFrameworkCore.Migrations.MigrationBuilder)")] diff --git a/SubitonAPI/SubitonAPI/Migrations/20191017170121_extendPhotos.cs b/SubitonAPI/SubitonAPI/Migrations/20191017170121_extendPhotos.cs index 1d880d0..6ff911f 100644 --- a/SubitonAPI/SubitonAPI/Migrations/20191017170121_extendPhotos.cs +++ b/SubitonAPI/SubitonAPI/Migrations/20191017170121_extendPhotos.cs @@ -4,6 +4,7 @@ namespace SubitonAPI.Migrations { public partial class extendPhotos : Migration { + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "")] protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( diff --git a/SubitonAPI/SubitonAPI/Migrations/20191102214503_add_name.cs b/SubitonAPI/SubitonAPI/Migrations/20191102214503_add_name.cs index 47fb605..3f5d635 100644 --- a/SubitonAPI/SubitonAPI/Migrations/20191102214503_add_name.cs +++ b/SubitonAPI/SubitonAPI/Migrations/20191102214503_add_name.cs @@ -2,8 +2,12 @@ namespace SubitonAPI.Migrations { + [System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "")] +#pragma warning disable CA1707 // Identifiers should not contain underscores public partial class add_name : Migration +#pragma warning restore CA1707 // Identifiers should not contain underscores { + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "")] protected override void Up(MigrationBuilder migrationBuilder) { migrationBuilder.AddColumn( @@ -19,6 +23,7 @@ protected override void Up(MigrationBuilder migrationBuilder) } + [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "")] protected override void Down(MigrationBuilder migrationBuilder) { migrationBuilder.DropColumn(