Ejemplo n.º 1
0
 private static void ValidatePosting(LegacyPosting posting)
 {
     if (posting.Amount == 0)
     {
         throw new ArgumentOutOfRangeException(nameof(posting.Amount));
     }
     if (string.IsNullOrEmpty(posting.Description))
     {
         throw new ArgumentOutOfRangeException(nameof(posting.Description));
     }
     if (posting.Date == new DateTime())
     {
         throw new ArgumentOutOfRangeException(nameof(posting.Date));
     }
     if ((posting.Latitude.HasValue ||
          posting.Longitude.HasValue ||
          posting.Accuracy.HasValue) &&
         (!posting.Latitude.HasValue ||
          !posting.Longitude.HasValue ||
          !posting.Accuracy.HasValue
         ))
     {
         throw new ArgumentException("Coordinates has to be all set or none set");
     }
 }
Ejemplo n.º 2
0
        public ActionResult <LegacyPosting> Put(LegacyPosting posting)
        {
            ValidatePosting(posting);

            var userId     = HttpContext.GetUserId();
            var newPosting = _postingRepository.Put(posting, userId);

            return(Ok(newPosting).AddLegacyFormatting());
        }
Ejemplo n.º 3
0
        public ActionResult <LegacyPosting> Post(LegacyPosting posting)
        {
            if (posting.Posting_id != 0)
            {
                throw new ArgumentOutOfRangeException(nameof(posting.Posting_id));
            }
            ValidatePosting(posting);

            var userId     = HttpContext.GetUserId();
            var newPosting = _postingRepository.Post(posting, userId);

            return(Ok(newPosting));
        }
Ejemplo n.º 4
0
        public LegacyPosting Post(LegacyPosting posting, int userId)
        {
            var subcategoryId = GetSubcategoryIdFromName(userId, posting.Description);

            var newPosting = new Posting
            {
                Amount        = posting.Amount,
                Description   = subcategoryId == null ? posting.Description : null,
                Date          = posting.Date.Date,
                Longitude     = posting.Longitude,
                Latitude      = posting.Latitude,
                Accuracy      = posting.Accuracy,
                UserId        = userId,
                CreatedOn     = DateTime.Now,
                SubcategoryId = subcategoryId,
            };

            _context.Posting.Add(newPosting);
            _context.SaveChanges();

            return(MapToLegacyPosting(posting.Description, newPosting));
        }
Ejemplo n.º 5
0
        public LegacyPosting Put(LegacyPosting posting, int userId)
        {
            var existingPosting = _context.PostingForUser(userId).FirstOrDefault(p => p.PostingId == posting.Posting_id);

            if (existingPosting == null)
            {
                throw new Exception($"Posting ({posting.Posting_id}) does not exists");
            }

            var subcategoryId = GetSubcategoryIdFromName(userId, posting.Description);

            existingPosting.Amount        = posting.Amount;
            existingPosting.Description   = subcategoryId == null ? posting.Description : null;
            existingPosting.SubcategoryId = subcategoryId;
            existingPosting.Date          = posting.Date;
            existingPosting.Longitude     = posting.Longitude;
            existingPosting.Latitude      = posting.Latitude;
            existingPosting.Accuracy      = posting.Accuracy;

            _context.SaveChanges();

            return(MapToLegacyPosting(posting.Description, existingPosting));
        }