Esempio n. 1
0
        /// <summary>
        /// Udførelse af kommandoen.
        /// </summary>
        /// <param name="command">Command til opdatering eller tilføjelse af budgetoplysninger.</param>
        /// <returns>Opdateret eller tilføjet budgetoplysninger.</returns>
        public BudgetoplysningerView Execute(BudgetoplysningerAddOrModifyCommand 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);
            }
            Budgetkonto budgetkonto;

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

            var budgetoplysninger = _finansstyringRepository.BudgetoplysningerModifyOrAdd(budgetkonto, command.År,
                                                                                          command.Måned,
                                                                                          command.Indtægter,
                                                                                          command.Udgifter);

            return(_objectMapper.Map <Budgetoplysninger, BudgetoplysningerView>(budgetoplysninger));
        }