<commit_before>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Votter.Data;
using Votter.Models;
using Votter.Data.Repositories;
using Votter.Data.Contracts;
using Votter.Services.Models;

namespace Votter.Services.Controllers
{
    public class PicturesController : ApiController
    {
        private static Random random = new Random();
        private VotterData data;

        public PicturesController()
        {
            data = new VotterData();
        }

        public IQueryable<PictureModel> GetPictures()
        {
            return this.data.Pictures
                            .All()
                            .Select(x => new PictureModel() 
                            { 
                                Id = x.PictureId, 
                                Link = x.Link, 
                                CategoryId = x.CategoryId, 
                                ApplicationUserId = x.ApplicationUserId.ToString()
                            });
        }

        public IQueryable<PictureModel> GetRandomPictureFromRandomCategory()
        {
            int randomCategory = GetRandomCategoryId();


            return this.data.Pictures
                            .All()
                            .Where(p => p.CategoryId == randomCategory)
                            .Select(x => new PictureModel()
                            {
                                Id = x.PictureId,
                                Link = x.Link,
                                CategoryId = x.CategoryId,
                                ApplicationUserId = x.ApplicationUserId
                            })
                            .Take(2);
        }

        [HttpGet]
        public IQueryable<PictureModel> GetTwoRandomPicsFromCategory(int categoryID)
        {

            return this.data.Pictures
                            .All()
                            .Where(p => p.CategoryId == categoryID)
                            .Select(x => new PictureModel()
                            {
                                Id = x.PictureId,
                                Link = x.Link,
                                CategoryId = x.CategoryId,
                                ApplicationUserId = x.ApplicationUserId
                            })
                            .Take(2);
        }

        [HttpGet]
        public IHttpActionResult GetPicture(int id)
        {
            PictureModel picture = data.Pictures.All()
                .Where(x=>x.PictureId == id)
                .Select(x => 
                    new PictureModel()
                                { 
                                    Id = x.PictureId, 
                                    Link = x.Link, 
                                    CategoryId = x.CategoryId,
                                    ApplicationUserId = x.ApplicationUserId.ToString()
                                })
                .FirstOrDefault();

            if (picture == null)
            {
                return NotFound();
            }

            return Ok(picture);
        }

        // PUT: api/Pictures/5
        //[ResponseType(typeof(void))]
        //public IHttpActionResult PutPicture(PictureModel picture)
        //{
        //    if (!ModelState.IsValid)
        //    {
        //        return BadRequest(ModelState);
        //    } 

        //    this.data.Pictures.Entry(picture).State = EntityState.Modified;

        //    try
        //    {
        //        this.data.SaveChanges();
        //    }
        //    catch (DbUpdateConcurrencyException)
        //    {
        //        if (!PictureExists(picture.Id))
        //        {
        //            return NotFound();
        //        }
        //        else
        //        {
        //            throw new ArgumentException("The picture Id wasn't valid");
        //        }
        //    }

        //    return StatusCode(HttpStatusCode.NoContent);
        //}
<commit_msg>Rafactored PicturesController<commit_after>﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using Votter.Data;
using Votter.Models;
using Votter.Data.Repositories;
using Votter.Data.Contracts;
using Votter.Services.Models;

namespace Votter.Services.Controllers
{
    public class PicturesController : ApiController
    {
        private static readonly Random random = new Random();
        private readonly VotterData data;

        public PicturesController()
        {
            data = new VotterData();
        }

        public IQueryable<PictureModel> GetPictures()
        {
            return this.data.Pictures
                            .All()
                            .Select(x => new PictureModel() 
                            { 
                                Id = x.PictureId, 
                                Link = x.Link, 
                                CategoryId = x.CategoryId, 
                                ApplicationUserId = x.ApplicationUserId.ToString()
                            });
        }

        [HttpGet]
        public IQueryable<PictureModel> GetTwoRandomPicsFromCategory(int categoryId)
        {
            return this.data.Pictures
                            .All()
                            .Where(p => p.CategoryId == categoryId)
                            .Select(x => new PictureModel()
                            {
                                Id = x.PictureId,
                                Link = x.Link,
                                CategoryId = x.CategoryId,
                                ApplicationUserId = x.ApplicationUserId
                            })
                            .Take(2);
        }

        [HttpGet]
        public IQueryable<PictureModel> GetRandomPictureFromRandomCategory()
        {
            int randomCategory = GetRandomCategoryId();
            return GetTwoRandomPicsFromCategory(randomCategory);
        }

        [HttpGet]
        public IHttpActionResult GetPicture(int id)
        {
            PictureModel picture = data.Pictures.All()
                .Where(x=>x.PictureId == id)
                .Select(x => 
                    new PictureModel()
                                { 
                                    Id = x.PictureId, 
                                    Link = x.Link, 
                                    CategoryId = x.CategoryId,
                                    ApplicationUserId = x.ApplicationUserId.ToString()
                                })
                .FirstOrDefault();

            if (picture == null)
            {
                return NotFound();
            }

            return Ok(picture);
        }

        [HttpPut]
        public IHttpActionResult PutPicture(PictureModel picture)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }

            //this.data.Pictures.Entry(picture).State = EntityState.Modified;

            try
            {
                this.data.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!PictureExists(picture.Id))
                {
                    return NotFound();
                }
                else
                {
                    throw new ArgumentException("The picture Id wasn't valid");
                }
            }

            return StatusCode(HttpStatusCode.NoContent);
        }
{'fraction_non_alphanumeric': 0.08163004598875831, 'fraction_numerical': 0.000510986203372509, 'mean_word_length': 1.0079507566042576, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4936416', 'n_tokens_mistral': 1879, 'n_tokens_neox': 1765, 'n_words': 334}