public HttpResponseMessage Add(
            string description,
            int categoryId,
            int subcategoryId,
            DateTime expiryDate,
            [FromBody] string userCredentials)
        {
            if (userCredentials == null)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "No user credentials."));
            }
            var loginPass = userCredentials.Split();

            if (loginPass.Length != 2)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Bad user credentials."));
            }
            try
            {
                var mapper = MapperDTO.Mapper;

                var category = mapper.Map <Category, Services.Model.Category>
                                   (new Category {
                    CategoryId = categoryId
                });
                var subcategory = mapper.Map <Subcategory, Services.Model.Subcategory>
                                      (new Subcategory {
                    SubcategoryId = subcategoryId
                });
                var user = mapper.Map <User, Services.Model.User>
                               (new User {
                    Login = loginPass[0], Password = loginPass[1]
                });

                Service.Add(description, expiryDate, user, category, subcategory);
                return(Request.CreateResponse(HttpStatusCode.Created));
            }
            catch (InvalidOperationException exc)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadRequest, exc.Message));
            }
            catch (ArgumentOutOfRangeException exc)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.InternalServerError, exc.Message));
            }
        }
示例#2
0
        private async Task AddLot(Dictionary <string, string> dictionary, string carId)
        {
            var lot = new Lot
            {
                State = StateEnum.Edit,
                ExternalDescription = dictionary[CarNameKey],
                ExternalLotNumber   = dictionary[TheCarsNumberKey],
                AuctionDate         = DateTime.Parse(dictionary[AuctionDateKey]),
                Vehicle             = new Vehicle
                {
                    Year = dictionary[RegistrationYearKey].Substring(0, 4),
                    EngineDisplacement = dictionary[DisplacementKey].Replace(" cc", ""),
                    Odometer           = int.Parse(dictionary[DistanceDrivenKey].Replace(",", "").Replace(" km", "")),
                    ExternalId         = carId
                }
            };

            await _lotService.Add(lot);
        }