Exemplo n.º 1
0
        public async Task <IResponseDTO> UpdateAttachmentType(int id, CreateUpdateAttachmentTypeDto options, int userId, int companyId)
        {
            try
            {
                // Validate Id
                var attachmentType = await _appDbContext.CompanyAttachmentTypes.AsNoTracking().FirstOrDefaultAsync(x => x.Id == id);

                if (attachmentType == null)
                {
                    _response.IsPassed = false;
                    _response.Errors.Add($"The specified attachment type id does not exist.");
                    return(_response);
                }
                else if (attachmentType.IsDeleted)
                {
                    _response.IsPassed = false;
                    _response.Errors.Add($"The specified attachment type '{attachmentType.Name}' is deleted.");
                    return(_response);
                }
                else
                {
                    attachmentType.UpdatedBy = userId;
                    attachmentType.UpdatedOn = DateTime.Now;
                    attachmentType.Name      = options.Name;
                }

                // Validate
                var validationResult = await ValidateAttachmentType(options, attachmentType, companyId, id);

                if (!validationResult.IsPassed)
                {
                    return(validationResult);
                }

                // save to the database
                _appDbContext.CompanyAttachmentTypes.Update(attachmentType);
                var save = await _appDbContext.SaveChangesAsync();

                if (save == 0)
                {
                    _response.IsPassed = false;
                    _response.Errors.Add("Database did not save the object");
                    return(_response);
                }

                _response.IsPassed = true;
                _response.Message  = "Attachment type is updated successfully";
            }
            catch (Exception ex)
            {
                _response.Data     = null;
                _response.IsPassed = false;
                _response.Errors.Add($"Error: {ex.Message}");
            }

            return(_response);
        }
Exemplo n.º 2
0
        // Validate
        private async Task <IResponseDTO> ValidateAttachmentType(CreateUpdateAttachmentTypeDto options, Data.DbModels.CompanySchema.CompanyAttachmentType attachmentType, int companyId, int id = 0)
        {
            try
            {
                // Validate Name
                if (_appDbContext.CompanyAttachmentTypes.Any(x => x.Name.Trim().ToLower() == options.Name.Trim().ToLower() && !x.IsDeleted && x.Company.Id == companyId && x.Id != id))
                {
                    _response.Errors.Add($"Attachment type name '{options.Name}' already exist, please try a new one.");
                }

                // Company
                var company = await _appDbContext.Companies.FirstOrDefaultAsync(x => x.Id == companyId);

                if (company == null)
                {
                    _response.Errors.Add($"The specified company id does not exist.");
                }
                else if (company.IsDeleted)
                {
                    _response.Errors.Add($"The specified company '{company.Name}' is deleted.");
                }
                else
                {
                    attachmentType.Company = company;
                }

                _response.IsPassed = true;
            }
            catch (Exception ex)
            {
                _response.IsPassed = false;
                _response.Data     = null;
                _response.Errors.Add($"Error: {ex.Message}");
            }

            if (_response.Errors.Count > 0)
            {
                _response.Errors   = _response.Errors.Distinct().ToList();
                _response.IsPassed = false;
                _response.Data     = null;
                return(_response);
            }

            return(_response);
        }
Exemplo n.º 3
0
        public async Task <IResponseDTO> CreateAttachmentType(CreateUpdateAttachmentTypeDto options, int userId, int companyId)
        {
            try
            {
                var attachmentType = _mapper.Map <Data.DbModels.CompanySchema.CompanyAttachmentType>(options);

                // Validate
                var validationResult = await ValidateAttachmentType(options, attachmentType, companyId);

                if (!validationResult.IsPassed)
                {
                    return(validationResult);
                }

                // Set the data
                attachmentType.CreatedBy = userId;
                attachmentType.CreatedOn = DateTime.Now;
                attachmentType.IsActive  = true;

                // save to the database
                await _appDbContext.CompanyAttachmentTypes.AddAsync(attachmentType);

                var save = await _appDbContext.SaveChangesAsync();

                if (save == 0)
                {
                    _response.IsPassed = false;
                    _response.Errors.Add("Database did not save the object");
                    return(_response);
                }

                _response.IsPassed = true;
                _response.Message  = "Attachment type is created successfully";
            }
            catch (Exception ex)
            {
                _response.Data     = null;
                _response.IsPassed = false;
                _response.Errors.Add($"Error: {ex.Message}");
            }

            return(_response);
        }
Exemplo n.º 4
0
        public async Task <IResponseDTO> UpdateAttachmentType([FromRoute] int id, [FromBody] CreateUpdateAttachmentTypeDto options)
        {
            _response = await _attachmentTypeService.UpdateAttachmentType(id, options, LoggedInUserId, LoggedInCompanyId);

            return(_response);
        }