Skip to content

Commit

Permalink
PhotoController initial
Browse files Browse the repository at this point in the history
PhotoController initial
  • Loading branch information
Oktawian-L committed Nov 9, 2019
1 parent 88c3657 commit 0f5516b
Show file tree
Hide file tree
Showing 7 changed files with 141 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,6 @@ SubitonBackend/.vs/SubitonBackend/v15/Server/sqlite3/storage.ide
SubitonAPI/.vs/SubitonAPI/DesignTimeBuild/.dtbcache
SubitonAPI/.vs/SubitonAPI/v16/Server/sqlite3/db.lock
SubitonAPI/.vs/SubitonAPI/v16/Server/sqlite3/storage.ide
SubitonAPI/SubitonAPI/appsettings.json
SubitonAPI/SubitonAPI/appsettings.json
SubitonAPI/SubitonAPI/appsettings.json
2 changes: 1 addition & 1 deletion SubitonAPI/.vs/SubitonAPI/config/applicationhost.config
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@
</site>
<site name="SubitonAPI" id="2">
<application path="/" applicationPool="SubitonAPI AppPool">
<virtualDirectory path="/" physicalPath="C:\GiT\Subiton-DEMO\SubitonAPI\SubitonAPI" />
<virtualDirectory path="/" physicalPath="C:\GiT\Subiton\SubitonAPI\SubitonAPI" />
</application>
<bindings>
<binding protocol="http" bindingInformation="*:50708:localhost" />
Expand Down
Binary file modified SubitonAPI/.vs/SubitonAPI/v16/TestStore/0/000-0000.testlog
Binary file not shown.
131 changes: 131 additions & 0 deletions SubitonAPI/SubitonAPI/Controllers/PhotosController.cs
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);
}
}
}
2 changes: 2 additions & 0 deletions SubitonAPI/SubitonAPI/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public void ConfigureServices(IServiceCollection services)
services.AddDbContext<DataContext>(x => x.UseSqlite("Data Source =Subiton.db"));
services.AddControllers();
services.AddCors();
//claudinary config
services.Configure<ClaudinarySettings>(Configuration.GetSection("ClaudinarySettings"));
//automapper init
services.AddAutoMapper(typeof(Startup));
// Auto Mapper Configurations
Expand Down
Binary file modified SubitonAPI/SubitonAPI/Subiton.db
Binary file not shown.
4 changes: 4 additions & 0 deletions SubitonAPI/SubitonAPI/SubitonAPI.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<PackageReference Include="CloudinaryDotNet" Version="1.8.0" />
<PackageReference Include="EntityFramework" Version="6.3.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.0.0" />
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.7">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="3.0.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand Down

0 comments on commit 0f5516b

Please sign in to comment.