<commit_before>﻿namespace ReaLocate.Web.Controllers
{
    using Data.Models;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using Services.Data;
    using Services.Data.Contracts;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Security;
    using ViewModels;

    [Authorize]
    public class AgenciesController : BaseController
    {
        private readonly IUsersService usersService;
        private readonly IAgenciesService agenciesService;
        private readonly IPaymentDetailsService paymentService;
        private readonly IUsersRolesService rolesService;

        public AgenciesController(IUsersService usersService, IAgenciesService agenciesService, 
            IPaymentDetailsService paymentService, IUsersRolesService rolesService)
        {
            this.usersService = usersService;
            this.agenciesService = agenciesService;
            this.paymentService = paymentService;
            this.rolesService = rolesService;
        }

        [HttpGet]
        public ActionResult CreateAgency()
        {
            var userId = this.User.Identity.GetUserId();
            var currentlyLoggedUser = this.usersService.GetUserDetails(userId);
            var roleId = currentlyLoggedUser.Roles.First().RoleId;
            var roleType = this.rolesService.GetRoleById(roleId).Name;

            if (roleType == "AgencyOwner")
            {
                return this.RedirectToAction("ErrorAgency", "Error");
            }
            else if(roleType == "Admin")
            {
                return this.RedirectToAction("ErrorAdmin", "Error");
            }
            else if(roleType == "Broker")
            {
                return this.RedirectToAction("ErrorBroker", "Error");
            }

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateAgency(CreateAgencyViewModel agency)
        {
            var userId = this.User.Identity.GetUserId();
            var currentlyLoggedUser = this.usersService.GetUserDetails(userId);

            var dbPaymentDetails = this.Mapper.Map<PaymentDetails>(agency.PaymentDetails);
            //save payment details
            this.paymentService.Add(dbPaymentDetails);

            var dbAgency = this.Mapper.Map<Agency>(agency);
            dbAgency.OwnerId = userId;
            dbAgency.Owner = currentlyLoggedUser;

            var newAgencyId = this.agenciesService.Add(dbAgency);
            var encodedId = this.agenciesService.EncodeId(newAgencyId);

            currentlyLoggedUser.MyOwnAgencyId = dbAgency.Id;
            currentlyLoggedUser.MyOwnAgency = dbAgency;<commit_msg>Added role count validation in agency controller<commit_after>﻿namespace ReaLocate.Web.Controllers
{
    using Data.Models;
    using Microsoft.AspNet.Identity;
    using Microsoft.AspNet.Identity.EntityFramework;
    using Services.Data;
    using Services.Data.Contracts;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Security;
    using ViewModels;

    [Authorize]
    public class AgenciesController : BaseController
    {
        private readonly IUsersService usersService;
        private readonly IAgenciesService agenciesService;
        private readonly IPaymentDetailsService paymentService;
        private readonly IUsersRolesService rolesService;

        public AgenciesController(IUsersService usersService, IAgenciesService agenciesService,
            IPaymentDetailsService paymentService, IUsersRolesService rolesService)
        {
            this.usersService = usersService;
            this.agenciesService = agenciesService;
            this.paymentService = paymentService;
            this.rolesService = rolesService;
        }

        [HttpGet]
        public ActionResult CreateAgency()
        {
            var userId = this.User.Identity.GetUserId();
            var currentlyLoggedUser = this.usersService.GetUserDetails(userId);
            var roleCount = currentlyLoggedUser.Roles.Count();

            if (roleCount != 0)
            {
                var roleId = currentlyLoggedUser.Roles.First().RoleId;
                var roleType = this.rolesService.GetRoleById(roleId).Name;

                if (roleType == "AgencyOwner")
                {
                    return this.RedirectToAction("ErrorAgency", "Error");
                }
                else if (roleType == "Admin")
                {
                    return this.RedirectToAction("ErrorAdmin", "Error");
                }
                else if (roleType == "Broker")
                {
                    return this.RedirectToAction("ErrorBroker", "Error");
                }

                return View();
            }

            return View();
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreateAgency(CreateAgencyViewModel agency)
        {
            var userId = this.User.Identity.GetUserId();
            var currentlyLoggedUser = this.usersService.GetUserDetails(userId);

            var dbPaymentDetails = this.Mapper.Map<PaymentDetails>(agency.PaymentDetails);
            //save payment details
            this.paymentService.Add(dbPaymentDetails);

            var dbAgency = this.Mapper.Map<Agency>(agency);
            dbAgency.OwnerId = userId;
            dbAgency.Owner = currentlyLoggedUser;

            var newAgencyId = this.agenciesService.Add(dbAgency);
            var encodedId = this.agenciesService.EncodeId(newAgencyId);

            currentlyLoggedUser.MyOwnAgencyId = dbAgency.Id;
            currentlyLoggedUser.MyOwnAgency = dbAgency;
{'fraction_non_alphanumeric': 0.0777015437392796, 'fraction_numerical': 0.00017152658662092623, 'mean_word_length': 2.1933187294633076, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6036110', 'n_tokens_mistral': 1473, 'n_tokens_neox': 1451, 'n_words': 287}