예제 #1
0
        public HttpResponseMessage Update(HttpRequestMessage request, ArticleDTO item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            var dbObject = Mapper.Map <Article>(item);

            if (_journalEditionRepository.GetById(dbObject.JournalEditionId) == null)
            {
                return(request.CreateErrorResponse(
                           HttpStatusCode.PreconditionFailed,
                           string.Format("Not found journal edition with id: {0}", dbObject.JournalEditionId)));
            }

            dbObject.Employees = null;

            _publicationBaseRepository.Update(dbObject);
            _db.SaveChanges();

            dbObject = (Article)_publicationBaseRepository.Get(x => x.Id == dbObject.Id, null, x => x.Employees).SingleOrDefault();

            foreach (var employee in dbObject.Employees.ToList())
            {
                dbObject.Employees.Remove(employee);
            }

            foreach (var employee in item.Employees)
            {
                var employeeToAdd = _employeeRepository.GetById(employee.Id);
                if (employeeToAdd == null)
                {
                    return(request.CreateErrorResponse(
                               HttpStatusCode.PreconditionFailed,
                               string.Format("Not found employee with id:{0}", employee.Id)));
                }
                dbObject.Employees.Add(employeeToAdd);
            }

            _db.SaveChanges();

            var jobs = new PublicationsJobs(dbObject);

            jobs.Start();

            var mapped = Mapper.Map <ArticleDTO>(dbObject);

            return(request.CreateResponse(HttpStatusCode.NoContent, mapped));
        }
예제 #2
0
        public HttpResponseMessage Add(HttpRequestMessage request, ArticleDTO item)
        {
            if (item == null)
            {
                throw new ArgumentNullException("item");
            }

            var dbObject = Mapper.Map <Article>(item);

            if (_journalEditionRepository.GetById(dbObject.JournalEditionId) == null)
            {
                return(request.CreateErrorResponse(
                           HttpStatusCode.PreconditionFailed,
                           string.Format("Not found journal edition with id: {0}", dbObject.JournalEditionId)));
            }

            dbObject.Employees = new List <Employee>();
            if (item.Employees != null)
            {
                foreach (var employee in item.Employees)
                {
                    var employeeToAdd = _employeeRepository.GetById(employee.Id);
                    if (employeeToAdd == null)
                    {
                        return(request.CreateErrorResponse(
                                   HttpStatusCode.PreconditionFailed,
                                   string.Format("Not found employee with id:{0}", employee.Id)));
                    }
                    dbObject.Employees.Add(employeeToAdd);
                }
            }

            _publicationBaseRepository.Insert(dbObject);
            _db.SaveChanges();

            var jobs = new PublicationsJobs(dbObject);

            jobs.Start();

            var mapped = Mapper.Map <ArticleDTO>(dbObject);

            return(request.CreateResponse(HttpStatusCode.Created, mapped));
        }