using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wierszowki.Core.Linq;
using xVal.ServerSide;

namespace Wierszowki.Services
{
    
    public class AuthorService : BaseService
    {
        public void Update(Author author)
        {
            var errors = DataAnnotationsValidationRunner.GetErrors(author);
            if (errors.Any())
                throw new RulesException(errors);

            var authorToUpdate = DataContext.GetAuthorById(author.Id);
            authorToUpdate.FirstName = author.FirstName;
            authorToUpdate.LastName = author.LastName;
            authorToUpdate.EmploymentTypeId = author.EmploymentTypeId;
            DataContext.SubmitChanges();                     
        }

        public void Create(Author author)
        {
            var errors = DataAnnotationsValidationRunner.GetErrors(author);
            if (errors.Any())
                throw new RulesException(errors);

            DataContext.Authors.InsertOnSubmit(author);
            DataContext.SubmitChanges();
        }
    }
}
