示例#1
0
        public async Task <IHttpActionResult> Save([StrongValidator(typeof(MaterialStimulationValidator))] MaterialStimulationDto dto)
        {
            var result = await _service.CallAsync(s => s.SaveAsync(dto));

            return(Ok(result));
        }
示例#2
0
        public async Task <MaterialStimulationDto> SaveAsync(MaterialStimulationDto dto)
        {
            if (dto == null)
            {
                throw new ArgumentNullException(nameof(dto));
            }

            using (var db = _dbFactory.Create(PersonalFilePermissions.EditOperation))
            {
                var personalFile = db.Set <PersonalFile>().SingleOrDefault(x => x.ID == dto.PersonalFileId);
                MaterialStimulation entity;
                if (dto.ID == Guid.Empty)
                {
                    entity = new MaterialStimulation
                    {
                        ID = Guid.NewGuid(),
                        RecordCreatedByUserID = _user.Id,
                        PersonalFileId        = dto.PersonalFileId,
                        OrganizationId        = personalFile.OrganizationId,
                    };

                    db.Set <MaterialStimulation>().Add(entity);
                }
                else
                {
                    entity = await db.Set <MaterialStimulation>()
                             .Include(x => x.OrderHead)
                             .SingleOrDefaultAsync(x => x.ID == dto.ID && !x.RecordDeleted.HasValue);

                    if (entity == null)
                    {
                        throw EntityNotFoundFault.Create(dto.ID, typeof(MaterialStimulation));
                    }

                    entity.RecordLastModifiedByUserID = _user.Id;
                    entity.RecordLastModified         = DateTime.Now;
                }

                if (entity.OrderHead == null)
                {
                    entity.OrderHead             = new OrderHead();
                    entity.RecordCreatedByUserID = _user.Id;

                    entity.OrderHead.OrderDate      = dto.OrderDate;
                    entity.OrderHead.OrderNumber    = dto.OrderNumber;
                    entity.OrderHead.OrganizationId = personalFile.OrganizationId;
                }
                else if (entity.OrderHead.OrderDate != dto.OrderDate ||
                         entity.OrderHead.OrderNumber != dto.OrderNumber)
                {
                    entity.OrderHead.OrderDate   = dto.OrderDate;
                    entity.OrderHead.OrderNumber = dto.OrderNumber;

                    entity.OrderHead.RecordLastModified         = DateTime.Now;
                    entity.OrderHead.RecordLastModifiedByUserID = _user.Id;
                }

                var binder = _binderFactory.Create <MaterialStimulation, MaterialStimulationDto>(db);
                entity = binder.BindFrom(entity, dto);

                await db.SaveChangesAsync();

                dto.ID = entity.ID;

                return(dto);
            }
        }