-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
141 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using AutoMapper; | ||
using CloudinaryDotNet; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.EntityFrameworkCore; | ||
using Microsoft.Extensions.Options; | ||
using SubitonAPI.Data; | ||
using SubitonAPI.Helpers; | ||
using SubitonAPI.Models; | ||
|
||
namespace SubitonAPI.Controllers | ||
{ | ||
[Authorize] | ||
[Route("api/[controller]")] | ||
[ApiController] | ||
public class PhotosController : ControllerBase | ||
{ | ||
private readonly DataContext _context; | ||
private readonly IUserRepository _userRepository; | ||
private readonly IMapper _mapper; | ||
private readonly IOptions<ClaudinarySettings> _claudinarySettings; | ||
private readonly Cloudinary _cloudinary; | ||
|
||
public PhotosController(DataContext context, IUserRepository userRepository, IMapper mapper, IOptions<ClaudinarySettings> claudinarySettings) | ||
{ | ||
_context = context; | ||
_userRepository = userRepository; | ||
_mapper = mapper; | ||
_claudinarySettings = claudinarySettings; | ||
|
||
Account account = new Account( | ||
_claudinarySettings.Value.CloudName, | ||
_claudinarySettings.Value.ApiKey, | ||
_claudinarySettings.Value.ApiSecret | ||
); | ||
|
||
_cloudinary = new Cloudinary(account); | ||
} | ||
|
||
// GET: api/Photos | ||
[HttpGet] | ||
public async Task<ActionResult<IEnumerable<Photo>>> GetPhotos() | ||
{ | ||
return await _context.Photos.ToListAsync(); | ||
} | ||
|
||
// GET: api/Photos/5 | ||
[HttpGet("{id}")] | ||
public async Task<ActionResult<Photo>> GetPhoto(int id) | ||
{ | ||
var photo = await _context.Photos.FindAsync(id); | ||
|
||
if (photo == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
return photo; | ||
} | ||
|
||
// PUT: api/Photos/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> PutPhoto(int id, Photo photo) | ||
{ | ||
if (id != photo.Id) | ||
{ | ||
return BadRequest(); | ||
} | ||
|
||
_context.Entry(photo).State = EntityState.Modified; | ||
|
||
try | ||
{ | ||
await _context.SaveChangesAsync(); | ||
} | ||
catch (DbUpdateConcurrencyException) | ||
{ | ||
if (!PhotoExists(id)) | ||
{ | ||
return NotFound(); | ||
} | ||
else | ||
{ | ||
throw; | ||
} | ||
} | ||
|
||
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(); | ||
|
||
return CreatedAtAction("GetPhoto", new { id = photo.Id }, photo); | ||
} | ||
|
||
// DELETE: api/Photos/5 | ||
[HttpDelete("{id}")] | ||
public async Task<ActionResult<Photo>> DeletePhoto(int id) | ||
{ | ||
var photo = await _context.Photos.FindAsync(id); | ||
if (photo == null) | ||
{ | ||
return NotFound(); | ||
} | ||
|
||
_context.Photos.Remove(photo); | ||
await _context.SaveChangesAsync(); | ||
|
||
return photo; | ||
} | ||
|
||
private bool PhotoExists(int id) | ||
{ | ||
return _context.Photos.Any(e => e.Id == id); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters