Beispiel #1
0
        public async Task Save([FromForm] MainBroadRequest input)
        {
            string fileLocation = UploadFile.CreateFolderIfNotExists(RootFolder, $@"{UploadFolder}\{MainBroadFolder}");

            if (input.Id.HasValue)
            {
                var data = await WorkScope.GetAll <MainBroad>().FirstOrDefaultAsync(x => x.Id == input.Id);

                if (data == null)
                {
                    throw new UserFriendlyException("Không tồn tại mainbroad");
                }

                if (input.Images?.Count > 0)
                {
                    var images = input.ImagePaths ?? new List <string>();
                    foreach (var item in input.Images)
                    {
                        string fileName = await UploadFile.UploadAsync(fileLocation, item);

                        images.Add($@"{UploadFolder}\{MainBroadFolder}\{fileName}");
                    }
                    data.ImageString = string.Join("-", images);
                }
                ObjectMapper.Map(input, data);
            }
            else
            {
                var data = ObjectMapper.Map <MainBroad>(input);
                if (input.Images?.Count > 0)
                {
                    var images = new List <string>();
                    foreach (var item in input.Images)
                    {
                        string fileName = await UploadFile.UploadAsync(fileLocation, item);

                        images.Add($@"{UploadFolder}\{MainBroadFolder}\{fileName}");
                    }
                    data.ImageString = string.Join("-", images);
                }
                await WorkScope.InsertAsync(data);
            }
        }