Ejemplo n.º 1
0
        /// <summary>
        /// Udførelse af kommandoen.
        /// </summary>
        /// <param name="command">Command til opdatering eller tilføjelse af kreditoplysninger.</param>
        /// <returns>Opdateret eller tilføjet kreditoplysninger.</returns>
        public KreditoplysningerView Execute(KreditoplysningerAddOrModifyCommand command)
        {
            if (command == null)
            {
                throw new ArgumentNullException("command");
            }

            Regnskab regnskab;

            try
            {
                var getBrevhoved = new Func <int, Brevhoved>(nummer => _fællesRepository.BrevhovedGetByNummer(nummer));
                regnskab = _finansstyringRepository.RegnskabGetAll(getBrevhoved)
                           .Single(m => m.Nummer == command.Regnskabsnummer);
            }
            catch (InvalidOperationException ex)
            {
                throw new DataAccessSystemException(
                          Resource.GetExceptionMessage(ExceptionMessage.CantFindUniqueRecordId, typeof(Regnskab),
                                                       command.Regnskabsnummer), ex);
            }
            Konto konto;

            try
            {
                konto = regnskab.Konti
                        .OfType <Konto>()
                        .Single(m => m.Kontonummer.CompareTo(command.Kontonummer) == 0);
            }
            catch (InvalidOperationException ex)
            {
                throw new DataAccessSystemException(
                          Resource.GetExceptionMessage(ExceptionMessage.CantFindUniqueRecordId, typeof(Konto),
                                                       command.Kontonummer), ex);
            }

            var kreditoplysninger = _finansstyringRepository.KreditoplysningerModifyOrAdd(konto, command.År,
                                                                                          command.Måned, command.Kredit);

            return(_objectMapper.Map <Kreditoplysninger, KreditoplysningerView>(kreditoplysninger));
        }