view Agendas/trunk/src/Agendas.Domain/Services/PersonaService.cs @ 144:a2b14da4902f

Alta y modificación de ponentes (personas)
author Nelo@Guinea.neluz.int
date Mon, 01 Aug 2011 10:25:23 -0300
parents 2dbb15f4510f
children 5a1f7233aa5a
line wrap: on
line source

using System;
using System.Collections.Generic;
using System.Linq;
using AltNetHispano.Agendas.Domain.Repositories;

namespace AltNetHispano.Agendas.Domain.Services
{
	public class PersonaService
	{
		private readonly IPersonaRepository _personaRepository;

		public PersonaService(IPersonaRepository personaRepository)
		{
			_personaRepository = personaRepository;
		}

		public bool Validate(IdentityProviderEnum identityProvider, string username)
		{
			var cuenta = _personaRepository.GetCuenta(identityProvider, username);
			return cuenta != null;
		}

		public bool CreateIfNotExist(IdentityProviderEnum identityProvider, string username, string nombre)
		{
			var cuenta = _personaRepository.GetCuenta(identityProvider, username);
            if (cuenta == null)
            {
                Persona persona = null;
                if (identityProvider == IdentityProviderEnum.Twitter)
                    persona = _personaRepository.GetByTwitter(username);
                
                if(persona==null)
                    persona = IdentityContext.IsAuthenticated() ? IdentityContext.GetUsuario() : new Persona(nombre);

                persona.AddCuenta(new Cuenta(identityProvider, username));

                if (identityProvider == IdentityProviderEnum.Twitter)
                    persona.Twitter = username;

                _personaRepository.Save(persona);
            }
		    return true;
		}

		public void AddCuenta(IdentityProviderEnum identityProvider, string username)
		{
			var persona = IdentityContext.GetUsuario();
			persona.AddCuenta(new Cuenta(identityProvider, username));

			if (identityProvider == IdentityProviderEnum.Twitter)
				persona.Twitter = username;
		}

		public void RemoveCuenta(IdentityProviderEnum identityProvider, string username)
		{
			var persona = IdentityContext.GetUsuario();
			var cuenta = persona.Cuentas.FirstOrDefault(c => c.IdentityProvider == identityProvider && c.LogonName == username);
			if (cuenta != null)
				persona.RemoveCuenta(cuenta);
		}

	    public IEnumerable<Persona> GetAll()
	    {
	        return _personaRepository.GetAll();
	    }

	    public Persona GetById(Guid id)
	    {
	        return _personaRepository.Get(id);
	    }

        public Resultado Add(string nombre, string twitter, string mail)
        {
            var persona = new Persona(nombre) {Twitter = twitter, Mail = mail};
            _personaRepository.Save(persona);

            return new Resultado(true);
        }

	    public Resultado Update(Guid id, string nombre, string twitter, string mail)
	    {
	        var persona = _personaRepository.Get(id);
            if (persona == null)
                return new Resultado(false)
                           {Message = string.Format("No se pudo encontrar la persona cuyo Id sea {0}", id)};

	        persona.Nombre = nombre;
	        persona.Twitter = twitter;
	        persona.Mail = mail;

	        return new Resultado(true);
	    }
	}
}