Example #1
0
        public IActionResult Post([Required][Image] IFormFile file)
        {
            var metadata = new FileMetadata
            {
                OriginalFileName = file.FileName,
                UploadDate       = DateTime.Now,
                FileType         = FileType.Image,
                OwnerId          = User.GetId()
            };

            var fileObj = new FileModel
            {
                Metadata = metadata,
                Data     = GetPreprocessedImage(file)
            };

            try
            {
                _db.StartTransaction();
                _db.FileMetadata.Create(metadata);
                _fs.Create(fileObj);
                _db.Commit();
            }
            catch
            {
                _db.Rollback();
                _fs.Delete(metadata);
                throw;
            }

            return(StatusCode((int)HttpStatusCode.Created, fileObj.Metadata.Id));
        }
Example #2
0
        public int Create(ContractDTO model, Role initiatorRole)
        {
            if (!ValidateContractForCreate(model, initiatorRole, out string error))
            {
                throw new EBValidationException(error);
            }

            try
            {
                db.StartTransaction();

                int createdId  = db.Contracts.Create(_mapper.Map <Contract>(model));
                var fullEntity = db.Contracts.Read(createdId);

                var report = GeneratePdfReport(_mapper.Map <ContractDTO>(fullEntity));
                fileRepository.Create(report);

                fullEntity.ReportId = report.Metadata.Id;
                db.Contracts.Update(fullEntity);

                db.Commit();
                return(createdId);
            }
            catch
            {
                db.Rollback();
                throw;
            }
        }
Example #3
0
        public ChatDTO CreateChat(ChatDTO chat)
        {
            try
            {
                _db.StartTransaction();
                var chatEntity = _mapper.Map <Chat>(chat);
                foreach (var u in chatEntity.Participants)
                {
                    u.AddedDate = DateTime.Now;
                }
                var chatId = _db.Chats.Create(chatEntity);
                _db.Commit();

                var created = GetChat(chatId);
                return(created);
            }
            catch
            {
                _db.Rollback();
                throw;
            }
        }
Example #4
0
        public IActionResult Post(AdvertDTO advert)
        {
            FileMetadata image = null;

            if (advert.ImageId != null)
            {
                image = _db.FileMetadata.Read(advert.ImageId.Value);
                if (image == null)
                {
                    return(NotFound($"Image {image.Id} not found!"));
                }
                else if (image.Assigned)
                {
                    return(Conflict($"Image {image.Id} is already used!"));
                }
                else if (image.OwnerId != User.GetId())
                {
                    return(Conflict($"Image {image.Id} is owned by another user!"));
                }
            }

            advert.UserId = User.GetId();
            var advertEntity = _mapper.Map <Advert>(advert);

            try
            {
                _db.StartTransaction();
                var advertId = _db.Adverts.Create(advertEntity);
                _db.Adverts.CreateFavorite(new AdvertFavorite
                {
                    UserId   = User.GetId(),
                    AdvertId = advertId
                });

                if (image != null)
                {
                    image.Assigned = true;
                    _db.FileMetadata.Update(image);
                }

                _db.Commit();
            }
            catch
            {
                _db.Rollback();
                throw;
            }

            return(Created(string.Empty, advertEntity.Id));
        }