Ejemplo n.º 1
0
        /// <summary>
        /// Adds the advertisement.
        /// </summary>
        /// <param name="request">The request.</param>
        /// <returns></returns>
        public JsonResponse AddAdvertisement(JsonRequest request)
        {
            if (request == null || request.AdvertisementCollection == null || request.AdvertisementCollection.Count == 0)
                return new JsonResponse { IsSuccessFull = false, Message = "Advertisement to add was empty!" };

            try
            {
                foreach (var advertisement in request.AdvertisementCollection)
                {
                    var validationResult = DatabaseContext.Entry(advertisement).GetValidationResult();
                    if (!validationResult.IsValid)
                    {
                        var entityValidationMessage = validationResult.ValidationErrors.FirstOrDefault();
                        return new JsonResponse
                        {
                            IsSuccessFull = false,
                            Message = entityValidationMessage != null
                                ? entityValidationMessage.ErrorMessage
                                : "The entity you are trying to save is NOT valid!"
                        };
                    }
                    DatabaseContext.AdvertisementCollection.Add(advertisement);
                }

                var result = DatabaseContext.SaveChanges();

               if (result < 1)
                   return new JsonResponse { IsSuccessFull = false, Message = "Error while saving new advertisement!" };

            }
            catch (Exception e) { return new JsonResponse { IsSuccessFull = false, Message = e.Message, Exception = e }; }
            return new JsonResponse { IsSuccessFull = true };
        }
Ejemplo n.º 2
0
        public JsonResponse ViewAllAdvertisementsByClient(JsonRequest request)
        {
            try
            {
                var client = request.ClientCollection.FirstOrDefault();
                if(client == null)
                    return new JsonResponse { Message = "The client was not defined!", IsSuccessFull = false };

                var advertisementCollection = DatabaseContext.AdvertisementCollection.Include(x => x.Client).Include(x => x.Category).ToList();
                advertisementCollection.Where(x => x.Client.Id == client.Id).ToList();

                if (advertisementCollection.FirstOrDefault() == null)
                    return new JsonResponse { Message = string.Format("No Advertisements in the database for client {0}!", client.Name), IsSuccessFull = false };

                var response = new JsonResponse
                {
                    IsSuccessFull = true,
                    Message = string.Format("Found {0} entries in the database!", advertisementCollection.Count),
                    AdvertisementCollection = advertisementCollection
                };
                return response;
            }
            catch (Exception e)
            {
                return new JsonResponse { Message = e.Message, Exception = e };
            }
        }