public async Task <IActionResult> Put([FromForm] AuctionAnnouncementModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (Request.Form.Files.Count() > 0)
                    {
                        var newAttachments = await ParseAttachmentsAsync(Request.Form.Files);

                        var allAttachments = model.Attachments.ToList();
                        allAttachments.AddRange(newAttachments);

                        model.Attachments = allAttachments;
                    }

                    await _announcementService.Edit(model, User.GetUserId());

                    return(Ok());
                }
                catch (Exception x)
                {
                    Log.Error($"ERROR Updating announcement with ID {model.Id}", x);
                    return(StatusCode(500, x.Message));
                }
            }

            return(BadRequest());
        }
        public async Task <IActionResult> Post([FromForm] AuctionAnnouncementModel model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    if (Request.Form.Files.Count() > 0)
                    {
                        model.Attachments = await ParseAttachmentsAsync(Request.Form.Files);
                    }

                    await _announcementService.Create(model, User.GetUserId());

                    return(Ok());
                }
                catch (Exception x)
                {
                    Log.Error("ERROR Creating announcement", x);
                    return(StatusCode(500, x.Message));
                }
            }

            return(BadRequest());
        }
        public async Task Create(AuctionAnnouncementModel model, string userId)
        {
            AuctionAnnouncement entry = new AuctionAnnouncement()
            {
                AuctionEndDate    = model.AuctionEndDate.ToUniversalTime(),
                AuctionStartDate  = model.AuctionStartDate.ToUniversalTime(),
                BidStep           = model.BidStep,
                Code              = model.Code,
                Description       = model.Description,
                Order             = model.Order,
                PropertyType      = model.PropertyType,
                RegisterEndDate   = model.RegisterEndDate.ToUniversalTime(),
                RegisterStartDate = model.RegisterStartDate.ToUniversalTime(),
                Title             = model.Title,
                StartPrice        = model.StartPrice,
                UserId            = userId
            };

            SetCreateStamp(entry, userId);
            _context.AuctionAnnouncement.Add(entry);

            foreach (var item in model.Items)
            {
                AuctionItem ai = new AuctionItem()
                {
                    Auction      = entry,
                    Description  = item.Description,
                    PropertyType = item.PropertyType
                };

                if (item.IsManuallyAdded)
                {
                    // Ръчно добавен
                    switch (Enum.Parse(typeof(Shared.Enums.PropertyType), item.ObjectType))
                    {
                    case Shared.Enums.NonNrzPropertyType.Asset:
                        ai.OtherPropertyId = item.NRZId;
                        break;

                    case Shared.Enums.NonNrzPropertyType.Property:
                        ai.RealEstateId = item.NRZId;
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    // Намерен от регистър на НРЗ
                    switch (Enum.Parse(typeof(Shared.Enums.PropertyType), item.PropertyType))
                    {
                    case Shared.Enums.PropertyType.AIRCRAFT:
                        ai.AircraftId = item.NRZId;
                        break;

                    case Shared.Enums.PropertyType.AGRIFORMACHINERY:
                        ai.AgriculturalMachinaryId = item.NRZId;
                        break;

                    case Shared.Enums.PropertyType.OTHER:
                        ai.OtherPropertyId = item.NRZId;
                        break;

                    case Shared.Enums.PropertyType.VEHICLE:
                        ai.VehicleId = item.NRZId;
                        break;

                    case Shared.Enums.PropertyType.REALESTATE:
                        ai.RealEstateId = item.NRZId;
                        break;

                    case Shared.Enums.PropertyType.VESSEL:
                        //TODO
                        break;

                    default:
                        break;
                    }
                }

                SetCreateStamp(ai, userId);
                _context.AuctionItem.Add(ai);
            }

            if (model.Attachments != null && model.Attachments.Any())
            {
                foreach (var item in model.Attachments)
                {
                    Attachment att = new Attachment()
                    {
                        FileName    = item.FileName,
                        ContentType = item.ContentType,
                        FileType    = item.FileName.Split('.').Last(),
                        Content     = item.Content
                    };
                    _context.Attachment.Add(att);
                    entry.AnnouncementAttachments.Add(new AnnouncementAttachments()
                    {
                        Attachment = att, Announcement = entry
                    });
                }
            }

            Auction auc = new Auction()
            {
                Announcement = entry,
                Completed    = false,
                BidStep      = entry.BidStep,
                Deleted      = false,
                EndDate      = entry.AuctionEndDate,
                StartDate    = entry.AuctionStartDate,
                StartPrice   = entry.StartPrice,
            };

            SetCreateStamp(auc, userId);

            await _context.Auction.AddAsync(auc);

            await _context.SaveChangesAsync();
        }
        public async Task Edit(AuctionAnnouncementModel model, string userId)
        {
            var dbEntry = await _context.AuctionAnnouncement.FindAsync(model.Id);

            if (dbEntry != null)
            {
                dbEntry.AuctionEndDate    = model.AuctionEndDate;
                dbEntry.AuctionStartDate  = model.AuctionStartDate;
                dbEntry.BidStep           = model.BidStep;
                dbEntry.Code              = model.Code;
                dbEntry.PropertyType      = model.PropertyType;
                dbEntry.RegisterEndDate   = model.RegisterEndDate;
                dbEntry.RegisterStartDate = model.RegisterStartDate;
                dbEntry.Title             = model.Title;
                dbEntry.StartPrice        = model.StartPrice;

                foreach (var item in model.Items)
                {
                    AuctionItem ai = new AuctionItem()
                    {
                        Auction      = dbEntry,
                        Description  = item.Description,
                        Nrzid        = item.NRZId,
                        PropertyType = item.PropertyType
                    };

                    SetCreateStamp(ai, userId);
                    _context.AuctionItem.Add(ai);
                }

                if (model.Attachments != null && model.Attachments.Count() > 0)
                {
                    //Remove attachments
                    var attToRemove = dbEntry.AnnouncementAttachments
                                      .Where(x => !model.Attachments.Any(y => y.Id == x.AttachmentId))
                                      .Select(x => x.Attachment);

                    _context.Attachment.RemoveRange(attToRemove);


                    //Add attachments
                    foreach (var attachment in model.Attachments)
                    {
                        if (attachment.Id == default)
                        {
                            //Add new attachment
                            Attachment att = new Attachment()
                            {
                                FileName    = attachment.FileName,
                                ContentType = attachment.ContentType,
                                FileType    = attachment.FileName.Split('.').Last(),
                                Content     = attachment.Content
                            };

                            await _context.Attachment.AddAsync(att);

                            await _context.AnnouncementAttachments.AddAsync(new AnnouncementAttachments()
                            {
                                Attachment   = att,
                                Announcement = dbEntry
                            });
                        }
                    }
                }
                else
                {
                    //remove all attachments
                    var attachments = dbEntry.AnnouncementAttachments.Select(x => x.Attachment);
                    _context.Attachment.RemoveRange(attachments);
                }

                await _context.SaveChangesAsync();
            }
        }