<commit_before>﻿namespace NeighboursCommunitySystem.API.Controllers
{
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;
    using System.Web.Http.Cors;
    using Breeze.WebApi2;
    using Common;
    using Server.DataTransferModels.Communities;
    using Data.DbContexts;
    using Server.Infrastructure.Validation;
    using Server.Common.Generators;
    using Server.Common.Constants;
    using Microsoft.AspNet.Identity;
    using System.Net.Http;
    using Microsoft.AspNet.Identity.Owin;
    using Services.Data.Contracts;
    using Models;

    [BreezeController]
    //[EnableCors(origins: "http://neighbourscommunityclient.azurewebsites.net, http://localhost:53074", headers: "*", methods: "*")]
    public class CommunitiesController : ApiController
    {
        private readonly ICommunitiesService communities;
        private ApplicationUserManager _userManager;

        public CommunitiesController(ICommunitiesService communities)
        {
            this.communities = communities;
        }

        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }

        [HttpGet]
        [EnableBreezeQuery]
        public IHttpActionResult Get()
        {
            var result = communities.All()
                .Select(c => new CommunityDataTransferModel()
                {
                    Name = c.Name,
                    Description = c.Description
                })
                .ToList();

            return this.Ok(result);
        }

        [HttpGet]
        public IHttpActionResult GetById(string userId)
        {
            var result = communities.All()
                .Where(c => c.Users.Select(u => u.Id).Contains(userId))
                .Select(c => new CommunityDataTransferModel()
                {
                    Name = c.Name,
                    Description = c.Description
                })

                .ToList();

            return this.Ok(result);
        }

        [ValidateModel]
        public async Task<IHttpActionResult> Post(CommunityWithAdminDataTransferModel model)
        {
            // Check if community with the same name already exists.
            if (this.communities.All().Any(c => c.Name == model.CommunityModel.Name))
            {
                return this.Ok("Community model with the same name already exists.");
            }

            // Create new community and store it in the database. 
            var newCommunityId = communities.Add(model.CommunityModel);
            var tokenGenerator = new RandomStringGenerator();
            var token = tokenGenerator.GetString(Server.Common.Constants.Constants.VerificationTokenLength) + model.CommunityModel.Name;

            var user = new User()
            {
                UserName = model.Email,
                Email = model.Email,
                FirstName = model.FirstName,<commit_msg>Added Post method for logged Admin<commit_after>﻿namespace NeighboursCommunitySystem.API.Controllers
{
    using System.Linq;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Http;
    using System.Web.Http.Cors;
    using Breeze.WebApi2;
    using Common;
    using Server.DataTransferModels.Communities;
    using Data.DbContexts;
    using Server.Infrastructure.Validation;
    using Server.Common.Generators;
    using Server.Common.Constants;
    using Microsoft.AspNet.Identity;
    using System.Net.Http;
    using Microsoft.AspNet.Identity.Owin;
    using Services.Data.Contracts;
    using Models;

    [BreezeController]
    //[EnableCors(origins: "http://neighbourscommunityclient.azurewebsites.net, http://localhost:53074", headers: "*", methods: "*")]
    public class CommunitiesController : ApiController
    {
        private readonly ICommunitiesService communities;
        private ApplicationUserManager _userManager;

        public CommunitiesController(ICommunitiesService communities)
        {
            this.communities = communities;
        }

        public ApplicationUserManager UserManager
        {
            get
            {
                return _userManager ?? Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
            }
            private set
            {
                _userManager = value;
            }
        }

        [HttpGet]
        [EnableBreezeQuery]
        public IHttpActionResult Get()
        {
            var result = communities.All()
                .Select(c => new CommunityDataTransferModel()
                {
                    Name = c.Name,
                    Description = c.Description
                })
                .ToList();

            return this.Ok(result);
        }

        [HttpGet]
        public IHttpActionResult GetById(string userId)
        {
            var result = communities.All()
                .Where(c => c.Users.Select(u => u.Id).Contains(userId))
                .Select(c => new CommunityDataTransferModel()
                {
                    Name = c.Name,
                    Description = c.Description
                })

                .ToList();

            return this.Ok(result);
        }

        [HttpPost]
        public IHttpActionResult PostCommunityByLoggedAdmin(CommunityDataTransferModel model)
        {
            // Check if community with the same name already exists.
            if (this.communities.All().Any(c => c.Name == model.Name))
            {
                return this.Ok("Community model with the same name already exists.");
            }

            if (ModelState.IsValid)
            {
                var newCommunityId = communities.Add(model);
                return this.Ok();
            }
            else
            {
                return this.BadRequest();
            }              
        }


        [ValidateModel]
        public async Task<IHttpActionResult> Post(CommunityWithAdminDataTransferModel model)
        {
            // Check if community with the same name already exists.
            if (this.communities.All().Any(c => c.Name == model.CommunityModel.Name))
            {
                return this.Ok("Community model with the same name already exists.");
            }

            // Create new community and store it in the database. 
            var newCommunityId = communities.Add(model.CommunityModel);
            var tokenGenerator = new RandomStringGenerator();
            var token = tokenGenerator.GetString(Server.Common.Constants.Constants.VerificationTokenLength) + model.CommunityModel.Name;

            var user = new User()
            {
                UserName = model.Email,
                Email = model.Email,
                FirstName = model.FirstName,
{'fraction_non_alphanumeric': 0.08425434189751686, 'fraction_numerical': 0.0017224056265250467, 'mean_word_length': 1.726134585289515, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, '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': '4047989', 'n_tokens_mistral': 1747, 'n_tokens_neox': 1659, 'n_words': 373}