Skip to content

Commit

Permalink
migration
Browse files Browse the repository at this point in the history
migration
  • Loading branch information
Oktawian-L committed Nov 9, 2019
1 parent cc99f0b commit 7f77f09
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 20 deletions.
5 changes: 4 additions & 1 deletion SubitonAPI/SubitonAPI/Controllers/AuthController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@ public AuthController(IAuthRepository repository, IConfiguration configuration)
public async Task<ActionResult> 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");
}
Expand Down
57 changes: 44 additions & 13 deletions SubitonAPI/SubitonAPI/Controllers/PhotosController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
/// <summary>
/// Photo uploaded to claudinary
/// </summary>
/// <seealso cref="Microsoft.AspNetCore.Mvc.ControllerBase" />
[Authorize]
[Route("api/[controller]")]
[ApiController]
public class PhotosController : ControllerBase
{
/// <summary>
/// Database context
/// </summary>
private readonly DataContext _context;

/// <summary>
/// The user repository
/// </summary>
private readonly IUserRepository _userRepository;

/// <summary>
/// The mapper
/// </summary>
private readonly IMapper _mapper;

/// <summary>
/// The claudinary
/// </summary>
private readonly IOptions<ClaudinarySettings> _claudinarySettings;

/// <summary>
/// The cloudinary
/// </summary>
private readonly Cloudinary _cloudinary;

public PhotosController(DataContext context, IUserRepository userRepository, IMapper mapper, IOptions<ClaudinarySettings> claudinarySettings)
Expand All @@ -42,6 +66,20 @@ public PhotosController(DataContext context, IUserRepository userRepository, IMa
_cloudinary = new Cloudinary(account);
}

// POST: api/Photos
[HttpPost]
public async Task<ActionResult> 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<ActionResult<IEnumerable<Photo>>> GetPhotos()
Expand Down Expand Up @@ -69,6 +107,10 @@ public async Task<ActionResult<Photo>> GetPhoto(int id)
[HttpPut("{id}")]
public async Task<IActionResult> PutPhoto(int id, Photo photo)
{

if (photo == null)
throw new ArgumentNullException(nameof(photo));

if (id != photo.Id)
{
return BadRequest();
Expand All @@ -78,7 +120,7 @@ public async Task<IActionResult> PutPhoto(int id, Photo photo)

try
{
await _context.SaveChangesAsync().ConfigureAwait(true);
await _context.SaveChangesAsync().ConfigureAwait(false);
}
catch (DbUpdateConcurrencyException)
{
Expand All @@ -95,17 +137,6 @@ public async Task<IActionResult> 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<ActionResult<Photo>> 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}")]
Expand All @@ -118,7 +149,7 @@ public async Task<ActionResult<Photo>> DeletePhoto(int id)
}

_context.Photos.Remove(photo);
await _context.SaveChangesAsync();
await _context.SaveChangesAsync().ConfigureAwait(true);

return photo;
}
Expand Down
4 changes: 2 additions & 2 deletions SubitonAPI/SubitonAPI/Controllers/ProfileController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ProfileController(DataContext context)
[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
{
return await _context.Users.ToListAsync();
return await _context.Users.ToListAsync().ConfigureAwait(false);
}

// GET: api/Users/5
Expand Down Expand Up @@ -97,7 +97,7 @@ public async Task<ActionResult<User>> DeleteUser(int id)
}

_context.Users.Remove(user);
await _context.SaveChangesAsync();
await _context.SaveChangesAsync().ConfigureAwait(false);

return user;
}
Expand Down
6 changes: 2 additions & 4 deletions SubitonAPI/SubitonAPI/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public UsersController(IUserRepository userRepository, IMapper mapper)
[HttpGet]
public async Task<ActionResult<IEnumerable<User>>> GetUsers()
{
var users = await _userRepository.GetAllUsers();
var users = await _userRepository.GetAllUsers().ConfigureAwait(false);
// map from user to copllection dto
var usersToReturn = _mapper.Map<IEnumerable<UserForDetailsDTO>>(users);

Expand All @@ -47,7 +47,7 @@ public async Task<ActionResult<IEnumerable<User>>> GetUsers()
[HttpGet("{id}")]
public async Task<ActionResult<User>> GetUser(int id)
{
var user = await _userRepository.GetUser(id);
var user = await _userRepository.GetUser(id).ConfigureAwait(false);

if (user == null)
{
Expand All @@ -59,8 +59,6 @@ public async Task<ActionResult<User>> 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<IActionResult> PutUser(int id, UserUpdateDTO userUpdate)
{
Expand Down
11 changes: 11 additions & 0 deletions SubitonAPI/SubitonAPI/DTO/PhotoCreateDTO.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace SubitonAPI.DTO
{
public class PhotoCreateDTO
{
}
}
6 changes: 6 additions & 0 deletions SubitonAPI/SubitonAPI/GlobalSuppressions.cs
Original file line number Diff line number Diff line change
@@ -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 = "<Pending>", Scope = "member", Target = "~M:SubitonAPI.Migrations.extendPhotos.Down(Microsoft.EntityFrameworkCore.Migrations.MigrationBuilder)")]
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace SubitonAPI.Migrations
{
public partial class extendPhotos : Migration
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "<Pending>")]
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<int>(
Expand Down
5 changes: 5 additions & 0 deletions SubitonAPI/SubitonAPI/Migrations/20191102214503_add_name.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

namespace SubitonAPI.Migrations
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "<Pending>")]
#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 = "<Pending>")]
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<string>(
Expand All @@ -19,6 +23,7 @@ protected override void Up(MigrationBuilder migrationBuilder)

}

[System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1062:Validate arguments of public methods", Justification = "<Pending>")]
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
Expand Down

0 comments on commit 7f77f09

Please sign in to comment.