Exemple #1
0
        public async Task <IActionResult> Edit(int id, [FromBody] FileDto fileDto)
        {
            if (fileDto == null)
            {
                return(BadRequest("You send an invalid file"));
            }

            var fileInDb = _fileService.Exist(id)
                           .Include(t => t.FileTags)
                           .Include(cp => cp.FileCompanies)
                           .Include(c => c.FileClubs).FirstOrDefault();

            if (fileInDb == null)
            {
                return(BadRequest("File doesn't exist!"));
            }

            var errorMsg = _fileService.CheckSameName(fileDto.Name);

            if (errorMsg != null)
            {
                return(BadRequest(errorMsg));
            }

            // Clear Relationships
            if (!_fileService.ClearRelations(fileInDb))
            {
                return(BadRequest("File can't be updated!"));
            }

            // Validate and Add Tags
            var tagErrMsg = _fileService.AddTagR(fileInDb, fileDto);

            if (!string.IsNullOrWhiteSpace(tagErrMsg))
            {
                return(BadRequest(tagErrMsg));
            }

            // Validate and Add Companies
            var companyErrMsg = _fileService.AddCompanyR(fileInDb, fileDto);

            if (!string.IsNullOrWhiteSpace(companyErrMsg))
            {
                return(BadRequest(companyErrMsg));
            }

            // Validate and Add Clubs
            var clubsErrMsg = _fileService.AddClubR(fileInDb, fileDto);

            if (!string.IsNullOrWhiteSpace(clubsErrMsg))
            {
                return(BadRequest(clubsErrMsg));
            }

            fileDto = _fileService.Edit(fileInDb, fileDto);

            var saved = await _fileService.SaveEf();

            if (!saved)
            {
                return(BadRequest("File can't be save!"));
            }

            return(Ok(_fileService.Exist(fileDto.Id).ProjectTo <FileDto>(_mapper.ConfigurationProvider).FirstOrDefault()));
        }