예제 #1
0
        public static ValidatorResult IsValidCourseCustomer(CourseCustomerCastDTO courseCustomerCast)
        {
            ValidatorResult validatorResult = new ValidatorResult(true);

            if (courseCustomerCast == null)
            {
                validatorResult.IsValid           = false;
                validatorResult.ValidationMessage = "The courseCustomerCast is null.";
                return(validatorResult);
            }

            if (courseCustomerCast.CourseId == 0)
            {
                validatorResult.IsValid           = false;
                validatorResult.ValidationMessage = "The courseId is required.";
                return(validatorResult);
            }
            if (courseCustomerCast.CustomerId.Trim().Equals(""))
            {
                validatorResult.IsValid           = false;
                validatorResult.ValidationMessage = "The customerId is required.";
                return(validatorResult);
            }
            return(validatorResult);
        }
        /// <summary>
        ///     Course cutomer pair creating
        /// </summary>
        /// <param name="courseCustomerCastDTO">Course customer model</param>
        /// <returns>
        ///    Returns the result message of the request.
        /// </returns>
        public HttpResponseMessage Post([FromBody] CourseCustomerCastDTO courseCustomerCastDTO)
        {
            var             response        = new HttpResponseMessage(HttpStatusCode.OK);
            ValidatorResult validatorResult = CourseCustomerValidator.IsValidCourseCustomer(courseCustomerCastDTO);

            if (!validatorResult.IsValid)
            {
                response.StatusCode = HttpStatusCode.PreconditionFailed;
                response.Content    = new StringContent(JsonConvert.SerializeObject(validatorResult.ValidationMessage));
                return(response);
            }

            var customer = northwindContext.Customers.Find(courseCustomerCastDTO.CustomerId);
            var course   = northwindContext.Courses.Find(courseCustomerCastDTO.CourseId);

            if (customer == null || course == null)
            {
                response.StatusCode = HttpStatusCode.PreconditionFailed;
                response.Content    = new StringContent(JsonConvert.SerializeObject("There is no course or customer with that id."));
                return(response);
            }
            var courseCustomer = northwindContext.CourseCustomerCasts.Where(cc => cc.CourseId == courseCustomerCastDTO.CourseId && cc.CustomerId == courseCustomerCastDTO.CustomerId).FirstOrDefault();

            if (courseCustomer != null)
            {
                response.StatusCode = HttpStatusCode.PreconditionFailed;
                response.Content    = new StringContent(JsonConvert.SerializeObject("This course customer pair is already exist."));
                return(response);
            }

            CourseCustomerCast courseCustomerCast = new CourseCustomerCast()
            {
                CourseId   = courseCustomerCastDTO.CourseId,
                CustomerId = courseCustomerCastDTO.CustomerId
            };

            //Add to DB
            try
            {
                northwindContext.Add(courseCustomerCast);
                northwindContext.SaveChanges();
                response.Content = new StringContent(JsonConvert.SerializeObject("The courseCustomer is successfully saved."));
                return(response);
            }
            catch (Exception e)
            {
                response.StatusCode = HttpStatusCode.InternalServerError;
                response.Content    = new StringContent(JsonConvert.SerializeObject(e.Message));
                return(response);
            }
        }
예제 #3
0
        public string Post([FromBody] CourseCustomerCastDTO courseCustomerCastDTO)
        {
            ValidatorResult validatorResult = CourseCustomerValidator.IsValidCourseCustomer(courseCustomerCastDTO);

            if (!validatorResult.IsValid)
            {
                HttpContext.Response.StatusCode = 422;
                return(JsonConvert.SerializeObject(validatorResult.ValidationMessage));
            }

            var customer = northwindContext.Customers.Find(courseCustomerCastDTO.CustomerId);
            var course   = northwindContext.Courses.Find(courseCustomerCastDTO.CourseId);

            if (customer == null || course == null)
            {
                HttpContext.Response.StatusCode = 422;
                return(JsonConvert.SerializeObject("There is no course or customer with that id."));
            }
            var courseCustomer = northwindContext.CourseCustomerCasts.Where(cc => cc.CourseId == courseCustomerCastDTO.CourseId && cc.CustomerId == courseCustomerCastDTO.CustomerId).FirstOrDefault();

            if (courseCustomer != null)
            {
                HttpContext.Response.StatusCode = 422;
                return(JsonConvert.SerializeObject("This course customer pair is already exist."));
            }

            CourseCustomerCast courseCustomerCast = new CourseCustomerCast()
            {
                CourseId   = courseCustomerCastDTO.CourseId,
                CustomerId = courseCustomerCastDTO.CustomerId
            };

            //Add to DB
            try
            {
                northwindContext.Add(courseCustomerCast);
                northwindContext.SaveChanges();
                HttpContext.Response.StatusCode = 200;
                return(JsonConvert.SerializeObject("The courseCustomer is successfully saved."));
            }
            catch (Exception e)
            {
                HttpContext.Response.StatusCode = 520;
                return(JsonConvert.SerializeObject(e.Message));
            }
        }