public IHttpActionResult PostService(Service service) { lock (syncLock) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } if (service.Impressions == null) { service.Impressions = new List <Impression>(); } if (service.Branches == null) { service.Branches = new List <Branch>(); } if (service.Vehicles == null) { service.Vehicles = new List <Vehicle>(); } unitOfWork.Services.Add(service); unitOfWork.Complete(); // notification ---------------------------------------------------------------------------------------- NotificationsHub.NotifyForService(++ServiceCount); return(CreatedAtRoute("DefaultApi", new { id = service.Id }, service)); } }
public IHttpActionResult PutService(Service service) { lock (syncLock) { if (!ModelState.IsValid) { return(BadRequest(ModelState)); } Service uS = unitOfWork.Services.Get(service.Id); // bez ovih 6 linija baca exception u Repository na update-u uS.Approved = service.Approved; // Attaching an entity of type 'X' failed because another entity of the same type already has the same primary key value uS.Description = service.Description; uS.Email = service.Email; uS.Logo = service.Logo; uS.Name = service.Name; try { unitOfWork.Services.Update(uS); unitOfWork.Complete(); } catch (DbUpdateConcurrencyException) { if (!ServiceExists(uS.Id)) { return(NotFound()); } else { throw; } } if (service.Approved == true) { MailMessage mail = new MailMessage("*****@*****.**", "*****@*****.**"); // drugi parametar service.Creator.Email umesto moje mejl adrese SmtpClient client = new SmtpClient(); client.Port = 587; client.DeliveryMethod = SmtpDeliveryMethod.Network; client.UseDefaultCredentials = false; client.Credentials = new NetworkCredential("*****@*****.**", "mitarsteva.12"); client.Host = "smtp.gmail.com"; client.EnableSsl = true; mail.Subject = "Service approved"; mail.Body = "The service that you have made has been approved by our administrators! \n You are now able to add vehicles and branches!"; //client.Send(mail); // notification ---------------------------------------------------------------------------------------- NotificationsHub.NotifyForService(--ServiceCount); } else { // notification ---------------------------------------------------------------------------------------- NotificationsHub.NotifyForService(++ServiceCount); } return(StatusCode(HttpStatusCode.NoContent)); } }
public IHttpActionResult DeleteService(int id) { var ser = unitOfWork.Services.Get(id); if (!ser.Approved) { // notification ---------------------------------------------------------------------------------------- NotificationsHub.NotifyForService(--ServiceCount); } var listOfRents = unitOfWork.Rents.GetAll(); List <Branch> listOfBranches = new List <Branch>(); foreach (Branch b in ser.Branches) { listOfBranches.Add(b); } foreach (Branch b in listOfBranches) { foreach (Rent r in listOfRents) { if (r.Branch1Id == b.Id || r.Branch2Id == b.Id) { if (r.Start <= DateTime.Now && r.End >= DateTime.Now) { return(BadRequest("Service is in use!")); } foreach (AppUser u in unitOfWork.AppUsers.GetAll()) { if (u.Rents.Remove(r)) { break; } } unitOfWork.Rents.Remove(r); } } unitOfWork.Branches.Remove(b); } List <Impression> impressions = new List <Impression>(); foreach (Impression i in ser.Impressions) { impressions.Add(i); } foreach (Impression i in impressions) { unitOfWork.Impressions.Remove(i); } List <Vehicle> vehicles = new List <Vehicle>(); foreach (Vehicle v in ser.Vehicles) { vehicles.Add(v); } foreach (Vehicle v in vehicles) { unitOfWork.Vehicles.Remove(v); } unitOfWork.Services.Remove(ser); unitOfWork.Complete(); return(Ok(ser)); }