Ejemplo n.º 1
0
        public async Task <OperationResult> Create(ItemsListPnModel model)
        {
            try
            {
                var core = await _coreHelper.GetCore();

                await using var dbContext = core.DbContextHelper.GetDbContext();

                var locale = await _userService.GetCurrentUserLocale();

                var language = dbContext.Languages.Single(x => x.LanguageCode.ToLower() == locale.ToLower());
                var template = await core.TemplateItemRead(model.RelatedEFormId, language);

                var itemsList = new ItemList
                {
                    Name             = model.Name,
                    Description      = model.Description,
                    CreatedByUserId  = _userService.UserId,
                    RepeatEvery      = model.RepeatEvery,
                    RepeatUntil      = model.RepeatUntil,
                    RepeatType       = model.RepeatType,
                    DayOfWeek        = model.DayOfWeek,
                    DayOfMonth       = model.DayOfMonth,
                    Enabled          = true,
                    RelatedEFormId   = model.RelatedEFormId,
                    RelatedEFormName = template?.Label
                };

                await itemsList.Create(_dbContext);

                foreach (var item in model.Items.Select(itemModel => new Item()
                {
                    LocationCode = itemModel.LocationCode,
                    ItemNumber = itemModel.ItemNumber,
                    Description = itemModel.Description,
                    Name = itemModel.Name,
                    Enabled = true,
                    BuildYear = itemModel.BuildYear,
                    Type = itemModel.Type,
                    ItemListId = itemsList.Id,
                    CreatedByUserId = _userService.UserId,
                }))
                {
                    await item.Create(_dbContext);
                }

                return(new OperationResult(
                           true,
                           _itemsPlanningLocalizationService.GetString("ListCreatedSuccessfully")));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                return(new OperationResult(false,
                                           _itemsPlanningLocalizationService.GetString("ErrorWhileCreatingList")));
            }
        }
 public async Task <OperationResult> Update([FromBody] ItemsListPnModel updateModel)
 {
     return(await _listService.Update(updateModel));
 }
 public async Task <OperationResult> Create([FromBody] ItemsListPnModel createModel)
 {
     return(await _listService.Create(createModel));
 }
        public async Task <OperationResult> Update(ItemsListPnModel updateModel)
        {
            try
            {
                var core = await _coreHelper.GetCore();

                await using var dbContext = core.DbContextHelper.GetDbContext();

                var locale = await _userService.GetCurrentUserLocale();

                Language language = dbContext.Languages.Single(x => x.LanguageCode.ToLower() == locale.ToLower());
                var      template = await _coreHelper.GetCore().Result.TemplateItemRead(updateModel.RelatedEFormId, language);

                var itemsList = new ItemList
                {
                    Id                    = updateModel.Id,
                    RepeatUntil           = updateModel.RepeatUntil,
                    RepeatEvery           = updateModel.RepeatEvery,
                    RepeatType            = updateModel.RepeatType,
                    DayOfWeek             = updateModel.DayOfWeek,
                    DayOfMonth            = updateModel.DayOfMonth,
                    Description           = updateModel.Description,
                    Name                  = updateModel.Name,
                    UpdatedAt             = DateTime.UtcNow,
                    UpdatedByUserId       = UserId,
                    RelatedEFormId        = updateModel.RelatedEFormId,
                    RelatedEFormName      = template?.Label,
                    LabelEnabled          = updateModel.LabelEnabled,
                    DescriptionEnabled    = updateModel.DescriptionEnabled,
                    DeployedAtEnabled     = updateModel.DeployedAtEnabled,
                    DoneAtEnabled         = updateModel.DoneAtEnabled,
                    DoneByUserNameEnabled = updateModel.DoneByUserNameEnabled,
                    UploadedDataEnabled   = updateModel.UploadedDataEnabled,
                    ItemNumberEnabled     = updateModel.ItemNumberEnabled,
                    LocationCodeEnabled   = updateModel.LocationCodeEnabled,
                    BuildYearEnabled      = updateModel.BuildYearEnabled,
                    TypeEnabled           = updateModel.TypeEnabled,
                    NumberOfImagesEnabled = updateModel.NumberOfImagesEnabled,
                    SdkFieldId1           = updateModel.SdkFieldId1,
                    SdkFieldId2           = updateModel.SdkFieldId2,
                    SdkFieldId3           = updateModel.SdkFieldId3,
                    SdkFieldId4           = updateModel.SdkFieldId4,
                    SdkFieldId5           = updateModel.SdkFieldId5,
                    SdkFieldId6           = updateModel.SdkFieldId6,
                    SdkFieldId7           = updateModel.SdkFieldId7,
                    SdkFieldId8           = updateModel.SdkFieldId8,
                    SdkFieldId9           = updateModel.SdkFieldId9,
                    SdkFieldId10          = updateModel.SdkFieldId10,
                    SdkFieldEnabled1      = updateModel.SdkFieldId1 != null,
                    SdkFieldEnabled2      = updateModel.SdkFieldId2 != null,
                    SdkFieldEnabled3      = updateModel.SdkFieldId3 != null,
                    SdkFieldEnabled4      = updateModel.SdkFieldId4 != null,
                    SdkFieldEnabled5      = updateModel.SdkFieldId5 != null,
                    SdkFieldEnabled6      = updateModel.SdkFieldId6 != null,
                    SdkFieldEnabled7      = updateModel.SdkFieldId7 != null,
                    SdkFieldEnabled8      = updateModel.SdkFieldId8 != null,
                    SdkFieldEnabled9      = updateModel.SdkFieldId9 != null,
                    SdkFieldEnabled10     = updateModel.SdkFieldId10 != null,
                    LastExecutedTime      = updateModel.LastExecutedTime
                };
                await itemsList.Update(_dbContext);

                // update current items
                var items = await _dbContext.Items
                            .Where(x => x.ItemListId == itemsList.Id)
                            .ToListAsync();

                foreach (var item in items)
                {
                    var itemModel = updateModel.Items.FirstOrDefault(x => x.Id == item.Id);
                    if (itemModel != null)
                    {
                        item.Description     = itemModel.Description;
                        item.ItemNumber      = itemModel.ItemNumber;
                        item.LocationCode    = itemModel.LocationCode;
                        item.Name            = itemModel.Name;
                        item.UpdatedAt       = DateTime.UtcNow;
                        item.UpdatedByUserId = UserId;
                        item.BuildYear       = itemModel.BuildYear;
                        item.Type            = itemModel.Type;
                        await item.Update(_dbContext);
                    }
                }

                // Remove old
                var itemModelIds   = updateModel.Items.Select(x => x.Id).ToArray();
                var itemsForRemove = await _dbContext.Items
                                     .Where(x => !itemModelIds.Contains(x.Id) && x.ItemListId == itemsList.Id)
                                     .ToListAsync();

                foreach (var itemForRemove in itemsForRemove)
                {
                    await itemForRemove.Delete(_dbContext);
                }

                // Create new
                foreach (var itemModel in updateModel.Items)
                {
                    var item = items.FirstOrDefault(x => x.Id == itemModel.Id);
                    if (item == null)
                    {
                        var newItem = new Item()
                        {
                            LocationCode    = itemModel.LocationCode,
                            ItemNumber      = itemModel.ItemNumber,
                            Description     = itemModel.Description,
                            Name            = itemModel.Name,
                            Version         = 1,
                            WorkflowState   = Constants.WorkflowStates.Created,
                            CreatedAt       = DateTime.UtcNow,
                            CreatedByUserId = UserId,
                            UpdatedAt       = DateTime.UtcNow,
                            Enabled         = true,
                            BuildYear       = itemModel.BuildYear,
                            Type            = itemModel.Type,
                            ItemListId      = itemsList.Id,
                        };
                        await newItem.Save(_dbContext);
                    }
                }

                return(new OperationResult(
                           true,
                           _itemsPlanningLocalizationService.GetString("ListUpdatedSuccessfully")));
            }
            catch (Exception e)
            {
                Trace.TraceError(e.Message);
                return(new OperationResult(
                           false,
                           _itemsPlanningLocalizationService.GetString("ErrorWhileUpdatingList")));
            }
        }