コード例 #1
0
        public IHttpActionResult PostCustomer([FromUri] string senderID, [FromBody] GeneralUserModel userModel)
        {
            Customer customer = new Customer(userModel.Username, userModel.Password)
            {
                FirstName = userModel.FirstName,
                LastName  = userModel.LastName,
                Gender    = userModel.Gender,
                JMBG      = userModel.JMBG,
                Phone     = userModel.Phone,
                Email     = userModel.Email,
            };

            userModel.TaxiDrivesIDs.ForEach(td => customer.TaxiDrives.Add(DbTaxiDrive.GetSingleEntityByKey(td)));

            //Customer ne pravi sam svoj nalog
            if (senderID != customer.Username)
            {
                if (!LoggedUsers.Contains(senderID))
                {
                    return(Content(HttpStatusCode.Unauthorized, "Not logged in."));
                }
                else if (!DbAdmin.Exists(senderID))
                {
                    return(Content(HttpStatusCode.Unauthorized, "Not a dispatcher nor the user to be added."));
                }
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            bool result;

            try
            {
                result = DbCustomer.Add(customer);
            }
            catch (Exception e)
            {
                Trace.Write($"Error on 'PostCustomer()'. Error message: {e.Message}");
                Trace.Write($"[STACK_TRACE] {e.StackTrace}");
                return(InternalServerError(e));
            }

            if (result)
            {
                return(Ok(customer));
            }
            else
            {
                return(BadRequest("Customer already exists."));
            }
        }