<commit_before>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Telerickr.Data;
using Telerickr.Models;
using Telerickr.Services.Models;

namespace Telerickr.Services.Controllers
{
    public class PhotosController : ApiController
    {
        private readonly IRepository<Photo> photos;

        public PhotosController()
        {
            var db = new TelerickrDbContext();
            this.photos = new GenericRepository<Photo>(db);
        }

        public IHttpActionResult Get()
        {
            var result = this.photos
                .All()
                .OrderByDescending(p => p.UploadDate)
                .Take(10)
                .Select(PhotoResponceModel.FromModel)
                .ToList();

            return this.Ok(result);
        }

        public IHttpActionResult Get(int id)
        {
            var result = this.photos
                .All()
                .Where(p => p.Id == id)
                .Select(PhotoResponceModel.FromModel)
                .FirstOrDefault();

            if (result == null)
            {
                return this.NotFound();
            }

            return this.Ok(result);
        }

        [Authorize]
        public IHttpActionResult Post(PhotoRequestModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newPhoto = new Photo
            {
                Title = model.Title,
                ImageUrl = model.ImageUrl,
                FileExtension = model.FileExtension,
                UploadDate = DateTime.Now,
                Likes = 0,
                AlbumId = model.AlbumId
            };

            this.photos.Add(newPhoto);
            this.photos.SaveChanges();

            return this.Ok(newPhoto.Id);
        }
    }
}<commit_msg>Add Delete for Photos.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Telerickr.Data;
using Telerickr.Models;
using Telerickr.Services.Models;

namespace Telerickr.Services.Controllers
{
    public class PhotosController : ApiController
    {
        private readonly IRepository<Photo> photos;

        public PhotosController()
        {
            var db = new TelerickrDbContext();
            this.photos = new GenericRepository<Photo>(db);
        }

        public IHttpActionResult Get()
        {
            var result = this.photos
                .All()
                .OrderByDescending(p => p.UploadDate)
                .Take(10)
                .Select(PhotoResponceModel.FromModel)
                .ToList();

            return this.Ok(result);
        }

        public IHttpActionResult Get(int id)
        {
            var result = this.photos
                .All()
                .Where(p => p.Id == id)
                .Select(PhotoResponceModel.FromModel)
                .FirstOrDefault();

            if (result == null)
            {
                return this.NotFound();
            }

            return this.Ok(result);
        }

        [Authorize]
        public IHttpActionResult Post(PhotoRequestModel model)
        {
            if (!this.ModelState.IsValid)
            {
                return this.BadRequest(this.ModelState);
            }

            var newPhoto = new Photo
            {
                Title = model.Title,
                ImageUrl = model.ImageUrl,
                FileExtension = model.FileExtension,
                UploadDate = DateTime.Now,
                Likes = 0,
                AlbumId = model.AlbumId
            };

            this.photos.Add(newPhoto);
            this.photos.SaveChanges();

            return this.Ok(newPhoto.Id);
        }

        public IHttpActionResult Delete(int id)
        {
            var result = this.photos
                .All()
                .FirstOrDefault(p => p.Id == id);

            if (result == null)
            {
                return this.NotFound();
            }
            
            // TODO: Add user validation.

            this.photos.Delete(result);
            this.photos.SaveChanges();

            return this.Ok("Photo deleted");
        }
    }
}
{'fraction_non_alphanumeric': 0.09067835717581911, 'fraction_numerical': 0.0013844023996308261, 'mean_word_length': 1.3495934959349594, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 12, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3137813', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1087, 'n_words': 228}