Esempio n. 1
0
        public void CreateAdvertisment(AdvertismentSaveModel saveModel)
        {
            try
            {
                // check the requester id
                string requesterId = _userService.GetUserIdForUserNameAsync(saveModel.UserName).Result;

                /**
                 * Check for existing advert name for user requesting operation
                 */
                if (requesterId == null ||
                    _adverismentRepository.GetAdvertimentByUserAndName(requesterId, saveModel.AdvertismentName) != null)
                {
                    throw new Exception("You have already added advertisment with such name");
                }

                // translate object
                Advertisment advert = new Advertisment
                {
                    AdvertismentName        = saveModel.AdvertismentName,
                    AdvertismentDescription = saveModel.Description,
                    InitialPrice            = saveModel.InitialPrice,
                    IsAuction       = saveModel.IsAuction,
                    IsBuyNow        = saveModel.IsBuyNow,
                    IsPromoted      = saveModel.IsPromoted,
                    BuyNowPrice     = saveModel.BuyNowPrice,
                    CurrentPrice    = saveModel.CurrentPrice,
                    CreatedDateTime = saveModel.CreatedDateTime,
                    CategoryId      = saveModel.CategoryId,
                    Status          = saveModel.Status,
                    UserId          = requesterId
                };

                // get the created advert id - will be required for saving attached photos
                int id = _adverismentRepository.CreateAdvertisment(advert);

                // check if advertisment contains any photos attached - if nothing found - finish
                if (saveModel.Photos == null || saveModel.Photos.Count <= 0)
                {
                    return;
                }
                List <AdvertPhotos> photos = new List <AdvertPhotos>();

                saveModel.Photos.ForEach(item =>
                {
                    photos.Add(new AdvertPhotos
                    {
                        AdvertismentId = id,
                        ContentLength  = item.ContentLength,
                        ContentType    = item.ContentType,
                        PhotoName      = item.Name,
                        UserId         = requesterId,
                        Content        = ConvertBase64StringToByteArray(item.Src),
                        Advertisment   = advert
                    });
                });

                // save the photos
                _adverismentRepository.SaveAdvertPhotos(photos);
            }
            catch (DbUpdateException ex)
            {
                Log.Warning(ex, AdvertismentConstants.CANNOT_SAVE_ADVERT);
                throw;
            }
            catch (Exception ex)
            {
                Log.Error(ex, ex.Message);
                throw;
            }
        }