Ejemplo n.º 1
0
        public async Task <IActionResult> UploadFile(int job_id, IFormFile file)
        {
            try
            {
                var job = await _context.Jobs.Include(c => c.Documents).FirstOrDefaultAsync(j => j.JobId == job_id);

                if (!User.HasClaim(claim => (claim.Type == DefinedClaimTypes.RecruiterId && claim.Value == job.RecruiterId.ToString()) ||
                                   (claim.Type == DefinedClaimTypes.Access && claim.Value == DefinedClaimAccessValues.Elevated)))
                {
                    return(BadRequest());
                }

                if (job.Documents == null)
                {
                    job.Documents = new List <FileRepresentationInDatabase>();
                }
                var document = await _savedDocumentHandler.SaveDocument(file);

                if (document == null)
                {
                    return(BadRequest());
                }

                _context.Documents.Add(document);
                _context.SaveChanges();
                var doc = _context.Documents.FirstOrDefault(d => d.FileRepresentationInDatabaseId == document.FileRepresentationInDatabaseId);
                if (doc == null)
                {
                    BadRequest();
                }
                job.Documents.Add(doc);
                _context.Entry(job).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                var debugEx = ex;
                return(BadRequest());
            }
        }
        public async Task <IActionResult> UploadFile(int client_id, IFormFile file)
        {
            try
            {
                var client = _context.CrmClients.Include(cl => cl.Documents).FirstOrDefault(cl => cl.ClientId == client_id);

                if (!User.HasClaim(claim => (claim.Type == DefinedClaimTypes.RecruiterId && claim.Value == client.RecruiterId.ToString()) ||
                                   (claim.Type == DefinedClaimTypes.Access && claim.Value == DefinedClaimAccessValues.Elevated)))
                {
                    return(BadRequest());
                }

                if (client.Documents == null)
                {
                    client.Documents = new List <FileRepresentationInDatabase>();
                }

                var document = await _savedDocumentHandler.SaveDocument(file);

                if (document == null)
                {
                    return(BadRequest());
                }
                _context.Documents.Add(document);
                _context.SaveChanges();
                client.Documents.Add(document);
                _context.Entry(client).State = EntityState.Modified;
                await _context.SaveChangesAsync();

                return(Ok());
            }
            catch (Exception ex)
            {
                return(BadRequest(ex.Message));
            }
        }