Skip to content

Commit

Permalink
PhotoManagementChallengeComplete
Browse files Browse the repository at this point in the history
  • Loading branch information
TryCatchLearn committed Sep 20, 2020
1 parent 713e2dc commit 49b4d34
Show file tree
Hide file tree
Showing 24 changed files with 722 additions and 28 deletions.
61 changes: 58 additions & 3 deletions API/Controllers/AdminController.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Linq;
using System.Threading.Tasks;
using API.Entities;
using API.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;
Expand All @@ -11,8 +12,13 @@ namespace API.Controllers
public class AdminController : BaseApiController
{
private readonly UserManager<AppUser> _userManager;
public AdminController(UserManager<AppUser> userManager)
private readonly IUnitOfWork _unitOfWork;
private readonly IPhotoService _photoService;
public AdminController(UserManager<AppUser> userManager, IUnitOfWork unitOfWork,
IPhotoService photoService)
{
_photoService = photoService;
_unitOfWork = unitOfWork;
_userManager = userManager;
}

Expand Down Expand Up @@ -60,9 +66,58 @@ public async Task<ActionResult> EditRoles(string username, [FromQuery] string ro

[Authorize(Policy = "ModeratePhotoRole")]
[HttpGet("photos-to-moderate")]
public ActionResult GetPhotosForModeration()
public async Task<ActionResult> GetPhotosForModeration()
{
return Ok("Admins or moderators can see this");
var photos = await _unitOfWork.PhotoRepository.GetUnapprovedPhotos();

return Ok(photos);
}

[Authorize(Policy = "ModeratePhotoRole")]
[HttpPost("approve-photo/{photoId}")]
public async Task<ActionResult> ApprovePhoto(int photoId)
{
var photo = await _unitOfWork.PhotoRepository.GetPhotoById(photoId);

if (photo == null) return NotFound("Could not find photo");

photo.IsApproved = true;

var user = await _unitOfWork.UserRepository.GetUserByPhotoId(photoId);

if (!user.Photos.Any(x => x.IsMain)) photo.IsMain = true;

await _unitOfWork.Complete();

return Ok();
}

[Authorize(Policy = "ModeratePhotoRole")]
[HttpPost("reject-photo/{photoId}")]
public async Task<ActionResult> RejectPhoto(int photoId)
{
var photo = await _unitOfWork.PhotoRepository.GetPhotoById(photoId);

if (photo == null) return NotFound("Could not find photo");

if (photo.PublicId != null)
{
var result = await _photoService.DeletePhotoAsync(photo.PublicId);

if (result.Result == "ok")
{
_unitOfWork.PhotoRepository.RemovePhoto(photo);
}
}
else
{
_unitOfWork.PhotoRepository.RemovePhoto(photo);
}

await _unitOfWork.Complete();

return Ok();
}

}
}
11 changes: 4 additions & 7 deletions API/Controllers/UsersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,10 @@ public async Task<ActionResult<IEnumerable<MemberDto>>> GetUsers([FromQuery] Use
[HttpGet("{username}", Name = "GetUser")]
public async Task<ActionResult<MemberDto>> GetUser(string username)
{
return await _unitOfWork.UserRepository.GetMemberAsync(username);
var currentUsername = User.GetUsername();
return await _unitOfWork.UserRepository.GetMemberAsync(username,
isCurrentUser: currentUsername == username
);
}

[HttpPut]
Expand Down Expand Up @@ -83,19 +86,13 @@ public async Task<ActionResult<PhotoDto>> AddPhoto(IFormFile file)
PublicId = result.PublicId
};

if (user.Photos.Count == 0)
{
photo.IsMain = true;
}

user.Photos.Add(photo);

if (await _unitOfWork.Complete())
{
return CreatedAtRoute("GetUser", new { username = user.UserName }, _mapper.Map<PhotoDto>(photo));
}


return BadRequest("Problem addding photo");
}

Expand Down
1 change: 1 addition & 0 deletions API/DTOs/PhotoDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@ public class PhotoDto
public int Id { get; set; }
public string Url { get; set; }
public bool IsMain { get; set; }
public bool IsApproved { get; set; }
}
}
10 changes: 10 additions & 0 deletions API/DTOs/PhotoForApprovalDto.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace API.DTOs
{
public class PhotoForApprovalDto
{
public int Id { get; set; }
public string Url { get; set; }
public string Username { get; set; }
public bool IsApproved { get; set; }
}
}
8 changes: 3 additions & 5 deletions API/Data/DataContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,14 @@ public DataContext(DbContextOptions options) : base(options)

public DbSet<UserLike> Likes { get; set; }
public DbSet<Message> Messages { get; set; }
public DbSet<Photo> Photos { get; set; }
public DbSet<Group> Groups { get; set; }
public DbSet<Connection> Connections { get; set; }

protected override void OnModelCreating(ModelBuilder builder)
{
base.OnModelCreating(builder);

builder.Entity<Group>()
.HasMany(x => x.Connections)
.WithOne()
.OnDelete(DeleteBehavior.Cascade);

builder.Entity<AppUser>()
.HasMany(ur => ur.UserRoles)
.WithOne(u => u.User)
Expand Down Expand Up @@ -69,6 +65,8 @@ protected override void OnModelCreating(ModelBuilder builder)
.WithMany(m => m.MessagesSent)
.OnDelete(DeleteBehavior.Restrict);

builder.Entity<Photo>().HasQueryFilter(p => p.IsApproved);

builder.ApplyUtcDateTimeConverter();
}
}
Expand Down
Loading

0 comments on commit 49b4d34

Please sign in to comment.